aspose file tools*
The moose likes Servlets and the fly likes Reading property files within a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading property files within a servlet" Watch "Reading property files within a servlet" New topic
Author

Reading property files within a servlet

Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
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

Joined: Nov 01, 2002
Posts: 363
Can you tell us what you are trying to do? Reading .properties files from a servlet is no different than anywhere else.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30924
    
158

Zak,
What path are your trying to use to read the property file?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
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

Joined: Sep 27, 2003
Posts: 126
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

Joined: Oct 12, 2000
Posts: 5093

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 ]

42
Dragos Haiduc
Greenhorn

Joined: Mar 05, 2004
Posts: 5
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

Joined: Mar 22, 2000
Posts: 12823
    
    5

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

Joined: Mar 05, 2004
Posts: 5
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

Joined: Sep 27, 2003
Posts: 126
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

Joined: Dec 10, 2003
Posts: 329

Hope this help.


SCJP, SCWCD, SCBCD, SCJD, BB Java2 and JSP1.1
Dragos Haiduc
Greenhorn

Joined: Mar 05, 2004
Posts: 5
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

Joined: Feb 20, 2001
Posts: 580
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, President<br />Carman Consulting, Inc.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading property files within a servlet