I want to be able to read some properties from a file in my servlet. I do not expect these properties to change very often at all, so I don't want to read the file with each request. I've decided to use a context listener so that the file is read in only one time when the servlet loads. From the context listener, what is the best way to make those properties accessible to the servlet class? Should it just be a static properties object in the listener class? I would like to keep it as simple as possible.
You can load the properties file just like this and store the properties object as the context attribute. Now retrieve the properties object into your servlet and then get your property as props.getProperty("") thats all. so that you don't need to loop thorough to set the properties.
InputStream inputStream = getServletContext().getResourceAsStream("/WEB-INF/app.properties"); Properties props = new Properties(); props.load(inputStream);