• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I set server root?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Grace Lo
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! It is working now!
 
reply
    Bookmark Topic Watch Topic
  • New Topic