This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet reading properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet reading properties file" Watch "servlet reading properties file" New topic
Author

servlet reading properties file

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Hi,
I am trying to get my servlet to load a properties file in the servlets init() method.
However the servlet complains that it cannot find the file. Does anyone know what directory the file should be in please? I am running tomcat, my web app is in a directory called 'helpdesk'. Many thanks...
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Try to change ur web.xml to look like this
<servlet>
<servlet-name>xyz</servlet-name>
<servlet-class>xyzServlet</servlet-class>
<init-param>
<param-name>properties</param-name>
<param-value>/WEB-INF/conf/helpdesk.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

put ur properties file at web-inf\conf\ directory.
Later in ur servlet u can read the init parameter like this.
String filename = getServletContext().getInitParameter("properties");

Hope this hlep


Spritle Software Blogs
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Is the following an alternative for the same?Please clarify:
Properties prop = new Properties();
prop.load(<FileInputStream> ;
and then reading the values using the usual
looping
Regards
Ken Robinson
Ranch Hand

Joined: Jul 25, 2002
Posts: 52
The code below is from production code I have running at works fine. The object sc is the ServletContext.

As in the example above, put the name of the .properties file as a parameter. I am using a ServletContextListener, so my parameter must be a context parameter. However, you could easily use a servlet with a servlet parameter.
Use the ServletContext.getResourceAsStream method as this allows the file to be found regardless of deploy type (.war or non-jar). Since this returns a stream and a properties object can load from a stream, it is a good match.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

The usual problem people have is because relative file paths don't work in a servlet environment.
However, it you use the Java Properties services, you can read the properties simply by naming them as though they were a Java class (which they might be anyway!).
for example, "com.mywebsite.myapp.Global" is a valid name for the resource contained in the file "WEB-INF/classes/com/mywebsite/myapp/Global.properties".


Customer surveys are for companies who didn't pay proper attention to begin with.
Ken Robinson
Ranch Hand

Joined: Jul 25, 2002
Posts: 52
The example I have listed works. As stated, it is in a production environment. Using the method you have just listed seems to have limitations. I would suggest leveraging what the system is required to do and what the Properties object, by design, provides to you.
While the getRealPath method has issues with getting a stream of a resource, I have never had an issue with getting a file using getResourceAsStream.
Think about it, packages such as STRUTS and Log4J must be able to get to their config files, which are usually stored under the WEB-INF directory. A relative path is supposed to work in the current context. If it does not, I doubt the product would have passed J2EE certification.
 
Don't get me started about those stupid light bulbs.
 
subject: servlet reading properties file