Issue
I'm having some issues with loading a (.properties) file from the webcontent folder in my servlet. The solutions that I've found don't seem to work. I'm getting an exception when trying to read the file. Below my folder and package structure and the parts of code.
Webcontent folder structure
WebContent
- WEB-INF
- languages
-- language.properties
- ....
Package structure
package x.y.z.aa
- Servlet.java
package x.y.z.ab
- PropertyLoader.java
Servlet.java code
public void init(ServletConfig config) throws ServletException {
super.init(config);
servletContext = this.getServletContext();
PropertyLoader = new PropertyLoader(servletContext);
}
PropertyLoader.java
public PropertyLoader(ServletContext context) {
super();
try{
properties.load(context.getResourceAsStream("/languages/language.properties"));
System.out.println(languages.get("test"));
} catch (Exception e){
System.out.println("Error reading properties file");
System.out.println(e.getMessage());
}
}
Sorry. I forgot to instantiate the Properties instance variable. Thanks for the tip regarding HTTP access when it's in its current location. This should be resolved when I put it under the WEB-INF Folder?
Solution
You should think about changing your properties file location. At the current location it's contents can be accesed through a simple HTTP GET request (as in http://host:8080/AppContext/languages/language.properties ), which could be considered as a security issue.
Also, if you could post the exception you get, could be from more help to find what's happening with your code.
Answered By - Tomas Narros
Answer Checked By - David Marino (JavaFixing Volunteer)