my dog learned polymorphism*
The moose likes Servlets and the fly likes Finding a servlet's actual path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Finding a servlet Watch "Finding a servlet New topic

Finding a servlet's actual path

paul wheaton

Joined: Dec 14, 1998
Posts: 20494

I have a file that goes with a servlet I wrote. I'm having trouble accessing the file from my servlet. Unless I hard code the path, I can't find it!
Is there a way to find out the actual path of my servlet, so I can use that path to specify where my file is?

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
This is one of the "gotchas" of servlet programming. I don't know exactly how you have set up your servlet server, but it will probably be similar enough to my example for you to make sense of it. My example uses graphic (gif) files, as they are the most common occurrence of this type of problem.
Compare a statically served file (say) http://myserver/directory/index.html
with a servlet installed at (say) http://myserver/servlets/myservlet
In the case of the static file, the URL includes enough local path information to locate all files in that directory. A piece of html like <img src="ugh.gif"> can locate the image by a simple replacement (remove the "leaf" of the URL and replace with the "ugh.gif") and still form a valid URL: http://myserver/directory/ugh.gif
In the servlet case, there is no equivalent local file path, so the same transformation would yield a URL like: http://myserver/servlets/ugh.gif
which isn't usually valid.
There are several ways round this:
1. Use absolute paths for images. Both local absolute paths (/images/ugh.gif) and full URLS (http://anotherserver/images/ugh.gif) work well, and with the full ones you can also use a different (faster?) server on a different machine or different port if you like.
2. Use relative paths to go up a level or two. In this case, something like <img src="../images/ugh.gif"> This usually works, but is difficult to maintain, as it assumes knowledge of the deployment URL in the servlet. It's tripped me up a few times.
3. Use the <base href> tag to pretend that the dynamically generated page was served from some other URL. This works well if the dynamic page only refers to static pages and files, but can have problems accessing other servlets.
4. Install your servlet at a URL which has a real path behind it. In the above axample, map the servlet to (say): http://myserver/directory/myservlet
This may not be possible on your servlet server, but it's a good trick if it is.
5. Install a little servlet at /servlets/ugh.gif which generates your file. This can be acceptable if the number of files is small, but can be infeasible for large amounts of files.
6. Install a servlet to match a certain file type, and locate the files from a specified place. This can be acceptable if all the static files you wish to access are of a small number of file types. It can also be a problem if you also wish to statically serve files of that type from another directory.
I hope some of these options have been useful. Let us know how you get on.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Re-reading your question, you may have been asking something else. That doesn't diminish the usefulness of the above, but it may not be quite so useful to you I can only ask for a little more detail in the question next time, yer honor.
Anyway. If you are trying to access a file from the java code within the servlet, then it's a little more tricky. You are right that it's not easy to find the "current directory" for a servlet. As in my above reply, there are several approaches you can try, though.
1. Temporarily modify your servlet to write instead of read a file, and give the file a nice, unique name. Then use your system search tools to find out where that file has been put. This may be the same place that it will be read from.
2. Put the files in your CLASSPATH and use getResourceAsStream (see Java in A Nutshell, pp211-215). This seems a little odd, but it usually works.
3. Don't hard code the path in the servlet code, but read it from an initialisation parameter. This is more configurable and flexible, but can cause problems if you forget to set the parameter when you install the servlet.
4. Hard code the path. But you know that.
Was this any use this time?
paul wheaton

Joined: Dec 14, 1998
Posts: 20494

I came up with a variation to #3 in your second message.
All of my stuff is in a "com tree" ( except for one class called LocalSystem. When code gets moved from computer to computer, the whole com tree is moved. LocalSystem is not. If you try to run anything without LocalSystem, you will find instructions on how to make LocalSystem.
LocalSystem has one method: getServletPath() that returns the hard coded path to where the servlets are stored.
Thanks for your help Frank.

robert crowther
Ranch Hand

Joined: Oct 19, 2013
Posts: 41
Very late after the original post, but I got a high-ranking hit looking for this info.

This is not perhaps what the original post asked (file? file placed where?) but I've seen the following method recommended elsewhere. This is a drastically stripped version of JForum's solution - servlet code thus,

appPath contains a path to wherever the app is currently mounted - I understand even if deployed/copied into another filesystem. So long as the target file is part of the transferred data (in WebContent, whatever, depends on the server), the value can be modified to burrow in.
It is sorta covered in the JavaRanch Style Guide.
subject: Finding a servlet's actual path
Similar Threads
write File to server with servlet
FileInputStream within a servlet - Path Question..
Setting classpath in iPlanet 4.1
Applet runs only if class file is in the same dir