views:

3164

answers:

5

I'm using Tomcat 5.5 as my servlet container. My web application deploys via .jar and has some resource files (textual files with strings and configuration parameters) located under its WEB-INF directory. Tomcat 5.5 runs on ubuntu linux. The resource file is read with a file reader:
fr = new FileReader("messages.properties");

The problem is that sometimes the servlet can't find the resource file, but if i restart it a couple of times it works, then again after some time it stops working. Can someone suggest what's the best way of reading resource strings from a servlet? Or a workaround for this problem? Putting the resource files under WEB-INF/classes doesn't help either.

+5  A: 

I'm guessing the problem is you're trying to use a relative path to access the file. Using absolute path should help (i.e. "/home/tomcat5/properties/messages.properties").

However, the usual solution to this problem is to use the getResourceAsStream method of the ClassLoader. Deploying the properties file to "WEB-INF/classes" will make it available to the class loader and you'll be able to access the properties stream.

Untested proto-code:

Properties props = new Properties();

InputStream is =
getClass().getClassLoader().getResourceAsStream("messages.properties");

props.load(is);
diciu
+2  A: 

If you use

new FileReader("message.properties");

Then the FileReader will attempt to read that file from the base directory - which in Tomcat is likely to be the /bin folder.

As diciu mentioned, use an absolute path or load it as a resource the classloader.

matt b
+1  A: 

If you are trying to access this file from a Servlet-aware class, such as a ContextListener or other lifecycle listener, you can use the ServletContext object to get the path to a resource.

These three are roughly equivalent. (Don't confuse getResourceAsStream as the same as the one provided by the ClassLoader class. They behave very differently)

void myFunc(ServletContext context) {
   //returns full path. Ex: C:\tomcat\5.5\webapps\myapp\web-inf\message.properties 
   String fullCanonicalPath = context.getRealPath("/WEB-INF/message.properties");

   //Returns a URL to the file. Ex: file://c:/tomcat..../message.properties
   URL urlToFile = context.getResource("/WEB-INF/message.properties");

   //Returns an input stream. Like calling getResource().openStream();
   InputStream inputStream = context.getResourceAsStream("/WEB-INF/message.properties");
   //do something
}
James Schek
A: 

I use the following code to load a properties file from within a servlet:

public void init(ServletConfig config) throws ServletException {
    String pathToFile = config.getServletContext().getRealPath("")
     + "/WEB-INF/config.properties";
    Properties properties = new Properties();
    properties.load(new FileInputStream(pathToPropertiesFile));
}

This works with Tomcat 6.0

Saulo Silva
A: 

I did used for Jboss Seam:

ServletLifecycle.getServletContext().getRealPath("")

Marcus Vinícius
what does this have to do with the question that was asked?
Jason S