• 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

Reading property files within a servlet

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before anyone says anything, I know that there are numerous posts
on this topic already. But I have tried them all, yet none work.
I have tried the getServletContext().getResourceAsStream() method == nada
I have tried modifying the web.xml file with my servlet parameters == nada.
Can anyone give me a definitive answer on how to achieve this in a clean and non-hardcoded path way?
Zak
scjp,scjd
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell us what you are trying to do? Reading .properties files from a servlet is no different than anywhere else.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zak,
What path are your trying to use to read the property file?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both of those work just fine if you get the syntax right. For example, I use the following:

Note the way the path is written, without a leading "/"
Here is an example of an init parameter in a <servlet> tag in web.xml

Bill
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I have:

Ok, I have a file in WEB-INF folder called servlet.properties.
I call the above code, and I get the result of NULL.
But whenever I use: /WEB-INF/.. I get a result, but when I call prop.load(..), the program hangs for some reason, and never gets past that call.
Can you explain?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

loads the properties file <your-web-app>/WEB-INF/classes/nl/esa/edhe/handlers.properties
Remember that the name is identical to classnaming, so relative to the root of the classpath (which for a webapp is WEB-IF/classes).
You might be getting null because your properties file is not on the classpath.
This is part of a singleton factory class. The reset method is included to make it possible to replace the propertiesfile at runtime without restarting the webapp (mainly for testing, but could be used by for example an installation routine as well when installing an upgrade to the application).
[ March 05, 2004: Message edited by: Jeroen Wenting ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what i do(i think the web application classloader(s) can't "see" past WEB-INF/classes). My properties file is placed in WEB-INF/classes.
My class loading this file is of course in a package under WEB-INF/classes.
File is named "jdbc.properties".
Here is the call:
Properties props = new Properties();
InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
try {
props.load(is);
is.close();
} catch (IOException ioe) {}
This always works, file can be modified when the servlet engine is running, a simple reload of my application context being enough for it to be able to use the modified properties file.
[ March 05, 2004: Message edited by: Dragos Haiduc ]
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please tell me that you dont really throw away that exception - the only chance you have for finding out why the load didn't work.
Bill
 
Dragos Haiduc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course i am treating properly that IOException.
However, i made the post simplified like earlier because i thought getting the InputStream for that property file was more important here.
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I add files to my classpath for the webserver to pick it up?
I have tried both of the above solutions, yet I still get NULL for
my inputstream, and MissingResourceException for the properties file.
I have set it up just as the above code snippets have illustrated.
Zak
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hope this help.
 
Dragos Haiduc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Zak Nixon:
How do I add files to my classpath for the webserver to pick it up?


Step 1. Place your properties file "fileName.properties" under the WEB-INF/classes folder of your web application.
Step 2. From a Java class of the same web application( this class must also be somewhere in WEB-INF/classes ) try the call:
InputStream is = getClass().getResourceAsStream("/fileName.properties");
This MUST work.
PS: Please provide more detailed info about your servlet-engine, if possible.
[ March 09, 2004: Message edited by: Dragos Haiduc ]
[ March 09, 2004: Message edited by: Dragos Haiduc ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He doesn't have to put the properties file on the classpath (in WEB-INF/classes). He is loading the properties file using the ServletContext, which is able to load resources relative to the context root.

Originally posted by Jeroen Wenting:

loads the properties file <your-web-app>/WEB-INF/classes/nl/esa/edhe/handlers.properties
Remember that the name is identical to classnaming, so relative to the root of the classpath (which for a webapp is WEB-IF/classes).
You might be getting null because your properties file is not on the classpath.
This is part of a singleton factory class. The reset method is included to make it possible to replace the propertiesfile at runtime without restarting the webapp (mainly for testing, but could be used by for example an installation routine as well when installing an upgrade to the application).
[ March 05, 2004: Message edited by: Jeroen Wenting ]

 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, try this. Create a JSP file...

Save that to the context root of any (currently working) webapp, such as jsp-examples which ships with Tomcat. Then, save a properties file called test.properties to the same context root. Make sure you put some properties in there. It WILL work. The "/" at the beginning of the resource URL passed in to ServletContext.getResourceAsStream() is REQUIRED.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic