tags:

views:

41

answers:

1

I have a properties file which I have put in the classpath and I am trying to load it from a JSP:

InputStream stream = application.getResourceAsStream("/alert.properties"); 
Properties props = new Properties(); 
props.load(stream); 

But I am getting a FileNotFoundException.

+1  A: 

The ServletContext#getResourceAsStream() returns resources from the webcontent, not from the classpath. You need ClassLoader#getResourceAsStream() instead.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
properties.load(classLoader.getResourceAsStream("filename.properties"));
// ...

That said, it is considered bad practice to write raw Java code like that in a JSP file. You should be doing that (in)directly inside a HttpServlet or maybe a ServletContextListener class.

BalusC
+1 - it is also inefficient to (I assume) repeatedly reload the same properties file each time a JSP is executed.
Stephen C