• 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

best place for .properties files in a Web App

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The alternative Simon mentioned was posted by Bill Brogden quite some time ago. I bookmarked it when I discovered it.

https://coderanch.com/t/348343/Servlets/java/relative-path-name-propertiesObject-load
 
Simon Brown
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic