File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet reading properties file" Watch "servlet reading properties file" New topic

servlet reading properties file

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
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

Joined: Jul 13, 2001
Posts: 3150
Try to change ur web.xml to look like this

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
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: 17280

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/".

An IDE is no substitute for an Intelligent Developer.
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.
I agree. Here's the link:
subject: servlet reading properties file
It's not a secret anymore!