This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading property files within a servlet

 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • 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
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zak,
What path are your trying to use to read the property file?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • 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 ]
 
Dragos Haiduc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Ken Boyd
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hope this help.
 
Dragos Haiduc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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 ]
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic