jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes How do I set server root? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I set server root?" Watch "How do I set server root?" New topic
Author

How do I set server root?

Grace Lo
Greenhorn

Joined: May 03, 2001
Posts: 12
Hi,
I would like to read a property file called "AAA" under c:\jakarta-tomcat-3.2.1\webapps\dayinlo02\
For some reason, my servlet always look for the file under c:\
Is there any place I can set server root?
Thanks alot,
Grace


Grace
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
Where exactly do you want to set your server root ? I'm not sure but there should be some .conf files what could be changed to acheive what you want..
Grace Lo
Greenhorn

Joined: May 03, 2001
Posts: 12
The root should be c:\jakarta-tomcat-3.2.1\webapps\dayinlo02\
Isn't it?
I have checked all conf files and still have no idea where to set the root.

Grace
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Off the top of my head, so no guarantees:

Alternatively, you could put your resource file in WEB-INF/classes/ and use getClass().getClassLoader().getResourceAsStream("AAA").
If you want to write to your resource file as well, you need to use files. Use getServletConfig().getServletContext().getRealPath("/AAA") to get your filename, then use the normal java.io file objects to read and write the file.
Whatever method you use, I would recommend storing your resource file somewhere under WEB-INF/ so an ordinary client cannot read it. Besides, all the other meta-information is there too.
- Peter
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This is one of the standard "gotchas" in the Servlet API. There are no rules as to what the "current directory" is set to when a servlet is running. In particular the current directory is not set to the root of a web application, which would seem to be the natural choice.
The reason is that a server may deploy a web application straight from a war or ear file (without expanding it to files), or even from a remote URL. None of these possibilities even have the concept of a "current directory", so attempting to set it or assume it is set to something would be very dangerous.
The usual solution for the likes of property files and other configurations is to put them in WEB-INF/classes and use ClassLoader.getResourceAsStream() to get an input stream to pass to Properties.load(). This is guaranteed to work, wherever your web application is stored.
Bear in mind, though, that although most current servlet containers will also allow you to open an OutputStream to a file found this way (because they expand wars and ears into a physical directory tree), this is a very dangerous thing to do. There is a crop of servlet containers coming which will deploy wars and ears without expanding them, and will not allow writing to "files" inside an archive.
Does this make sense?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Grace Lo
Greenhorn

Joined: May 03, 2001
Posts: 12
Thanks a lot for replying me.
I did try getServletContext().getResourceAsStream("AAA"));
but when I compiled the servlet, it gave me an error saying "non-static method getServletContext() can't be reference from a static context."
The problem is I have to make getDriver() static in order to be called by init() from another servlet.
------------------------------------
public void init(ServletConfig config) {
super.init(config);

String driver = DriverUtilities.getDriver();
}
------------------------------------
public class DriverUtilities extends HttpServlet {
private static Properties pr=new Properties();
public static String getDriver() {
try {
pr.load(getServletContext().getResourceAsStream("AAA"));
} catch(Exception e) {
System.err.println("Error read AAA: " + e);
}
return(pr.getProperty("driver"));
}
}
------------------------------
Another thing is that I have tried to use ResourceBundle like following codes:
private static ResourceBundle bundle=null;
bundle=ResourceBundle.getBundle("AAA");
System.out.println(bundle.getString("driver"));
and set classpath to where AAA is.
It is working but I am wondering if it is a right way to do it.
-------------------------------------
By the way, is there any way to specify where I want to write data into a property file??

Thanks a lot,
Grace
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
"non-static method getServletContext() can't be reference from a static context." The problem is I have to make getDriver() static in order to be called by init() from another servlet.

As it happens, the ServletConfig object is being passed to the init() method, so you can simply include ServletContext as a parameter in getDriver(). In your init method, you would write something like getDriver(config.getServletContext()).
bundle=ResourceBundle.getBundle("AAA");
Now you have me confused. Do you want a resource bundle, a properties file, or both? Anyway, ResourceBundle is an abstract class, so your code won't work. I suggest you use a PropertyResourceBundle, which has a constructor taking an InputStream. You can get this InputStream in any of the ways described in the replies above.
NB: regarding Frank's point, both ServletContext.getResourceAsStream() and ClassLoader.getResourceAsStream() should work fine from an archive. As he points out, using files won't work; getRealPath() would return null in such cases.
- Peter
Grace Lo
Greenhorn

Joined: May 03, 2001
Posts: 12
Thanks a lot! It is working now!
 
GeeCON Prague 2014
 
subject: How do I set server root?