aspose file tools*
The moose likes Servlets and the fly likes best place for .properties files in a Web App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "best place for .properties files in a Web App" Watch "best place for .properties files in a Web App" New topic
Author

best place for .properties files in a Web App

Michael OBrien
Greenhorn

Joined: Apr 25, 2002
Posts: 19
Lets suppose I have some class (. ) located in my servlet context named FooServlet
So I have:
FooServlet/foo.jsp
FooServlet/web-inf/classes/com/foo/util/Use.class
And, inside of foo.jsp I am create an instance of "Use.class"
Then in a method in Use.class, I would like to fetch some configuration data from a moo.properties file located at
FooServlet/web-inf/properties/moo.properties.
What would the path be to moo.properties and how might I go about opening this file?
Michael OBrien
Greenhorn

Joined: Apr 25, 2002
Posts: 19
so the servlet 2.3 spec reads:
"The ServletContext interface provides direct access to the hierarchy of static content documents that are part of the web application, including HTML, GIF, and JPEG files, via the following methods of the ServletContext interface:
getResource
getResourceAsStream
The getResource and getResourceAsStream take a String with a leading "/" as argument which gives the path of the resource relative to the root of the context."
So in my example, I might call:
ServletContext sc = new ServletContext;
InputStream props = new InputStream;
props = sc.getResourceAsStream("/properties/moo.properties");
Right?
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
That's right, although (off the top of my head) I think that you need to include the "/WEB-INF" bit too.
Also, and if that bean might be used outside of the Servlet environment, you could load the properties file via the classloader, using something like getClass().getResourceAsStream("myProperties.properties").
Hope that helps...
Simon
Michael OBrien
Greenhorn

Joined: Apr 25, 2002
Posts: 19
Simon,
Thanks...and yes you are correct. The folder web-inf would be needed as well. Your alternative sounds interesting. I am going to explore that a bit to make even more portable classes.
Thanks!
Mike
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The alternative Simon mentioned was posted by Bill Brogden quite some time ago. I bookmarked it when I discovered it.

http://www.coderanch.com/t/348343/Servlets/java/relative-path-name-propertiesObject-load
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Indeed, it's a great way of shipping properties files since you can include them inside the JAR file. It also means that it is easy to override the settings because you can just prepend another location/directory/JAR file to your classpath.
If you want to access resources that sit next to your class (in the same package structure), then use "myProperties.properties", otherwise you can reference resources elsewhere by using "/myProperties.properties". The latter simply looks from the "top" of the classpath rather than in the same package.
Cheers
Simon
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
In many cases, I would use Class.getResource to load properties files in preference to the ServletContext one, as that way it works exactly like a normal application, you can package your properties in a jar, etc.
The CLASSPATH method of overriding properties won't work in a web-app context, as the classloader generally prefers classes in the web-app WAR over those in the CLASSPATH (SRV.9.7.2).
But all is not lost. The Servlet specification mandates that WEB-INF/classes comes before the jars in WEB-INF/lib in the classloader chain (SRV.9.5). As a consequence, if there is a default .properties file hiding inside a jar in WEB-INF/lib, you can override it simply by putting a .properties in the equivalent location in WEB-INF/classes. You shouldn't even have to fiddle your CLASSPATH.
A word of warning, although this works for me, this seems to me one of those areas where servers may subtly deviate from the spec.
- Peter
[ May 09, 2002: Message edited by: Peter den Haan ]
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Originally posted by Peter den Haan:
A word of warning, although this works for me, this seems to me one of those areas where servers may subtly deviate from the spec.

Don't you just hate it when that happens! :roll:
Simon
Michael OBrien
Greenhorn

Joined: Apr 25, 2002
Posts: 19
Hi All,
Thanks for the great input. One more good reason to store the .properties with the class that uses it...it makes Junit testing a hell of a lot easier.
For example...if your class is doing DB work and your store a jdbc url in the properties file and eventually toss it in the servlet context, then you can't really build STAND ALONE unit tests to exercise the class methods.
Testing the classes outside of the container proves a bit simpler, especially if some developers are writing jsp/html and some are building beans.
 
wood burning stoves
 
subject: best place for .properties files in a Web App