|
Answer» • Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes DIRECTORY or in a jar file contained in the WEB-INF/lib directory.
• Another way is to use the method getResourceAsStream() from the ServletContext class. This allows you update the file without having to RELOAD the webapp as required by the first method. Here is an example code snippet, without any error trapping:
// Assuming you are in a Servlet EXTENDING HTTPSERVLET
// This will look for a file called "/more/cowbell.properties" relative
// to your servlet ROOT Context
InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties");
Properties p = new Properties();
p.load(is);
is.close(); • Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.
• Another way is to use the method getResourceAsStream() from the ServletContext class. This allows you update the file without having to reload the webapp as required by the first method. Here is an example code snippet, without any error trapping: // Assuming you are in a Servlet extending HttpServlet
// This will look for a file called "/more/cowbell.properties" relative
// to your servlet Root Context
InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties");
Properties p = new Properties();
p.load(is);
is.close();
|