• Post Reply Bookmark Topic Watch Topic
  • New Topic

help keep me out of the rubber room  RSS feed

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble getting a servlet to find a file on the local file system.

Running Tomcat on my workstation, here is the complete directory structure for the file I am trying to get:

C:\Program Files\jakarta\jakarta-tomcat\webapps\Minnaqua\html\lite\minnaqua\volunteerTimeFrameset.html

here is the path the servlet is trying to use to find that file:
"/Minnaqua/html/lite/minnaqua/volunteerTimeFrameset.html"

The servlet itself is here:
C:\Program Files\jakarta\jakarta-tomcat\webapps\Minnaqua\WEB-INF\classes\Minnaqua\minnaqua.class

what confuses me is that when I tell the servlet to report it's current directory with getRealPath() , it gives me
C:\Documents and Settings\timcguir\Start Menu\Programs\Apache Tomcat 4.1

So, tell me, why would the servlet be looking in this directory?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try dropping the context when you refer to the file (I assume you're doing it through a RequestDispatcher?). The support is sometimes different depending on the servlet container, some need you to pass the context, some don't.

I assume you're starting Tomcat form a link on the start menu? This would cause the 'current directory' to the location you see in the getRealPath(), but no idea why it's returning that directory rather than the correct one. Question for the ages, I guess.

Hope this helps,
Dave.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never do anything in servlet code that depends on the "current directory" - either supply a complete path as an init parameter or use the ServletContext methods such as getRealPath, getResourceAsStream, etc.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!