userimage

Where to place and how to read properties files in a JSP/Servlet web application?

In my web application I need to put labels in properties files, so I wish to add that to a .properties file and access it when required.

userimage

Put it in the classpath, so that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path:


ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

Properties properties = new Properties();

properties.load(classLoader.getResourceAsStream("myProperty.properties"));


Here myProperty.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. webapp's /WEB-INF/lib and /WEB-INF/classes,If the propertiesfile is webapp-specific, best is to place it in /WEB-INF/classes.


If you're using a Maven project, drop it in /main/resources folder.

If you have placed the myProperty.properties it in a Java package structure like com.example, then you need to load it as below
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

Properties properties = new Properties();properties.load(classLoader.getResourceAsStream("com/Soft/myProperty.properties"));

Answer is