How to load a properties file placed in a classpath from JSP?

I have a properties file that I put in the classpath and I'm trying to load it from JSP:

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

But I get it FileNotFoundException.

+3
source share
1 answer

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

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

However, it is considered bad practice to write Java source code similar to this in a JSP file. You should do this (in) directly inside the class HttpServletor maybe ServletContextListener.

+2
source

Source: https://habr.com/ru/post/1747825/


All Articles