JSP That Reads and Displays a Properties File

One way to read properties at initialization is to read properties from web.xml, as described in "Servlet That Displays Its Initial Parms From web.xml". The properties in web.xml are in 'xml' format.

Another way - and one must admit, an older way - to keep properties is in a properties file, which generally looks something like this, with the name of a property and then the current value of that property, with an 'equal sign' in between. This is also somethings called the 'key' and 'value'.


The default location for such a properties file is the 'root directory of the web application', which is the same level as the WEB-INF directory is on.

So let's say that you put such properties in a file called "samp.properties". Here is a JSP (it could also easily be made into a Servlet) that reads and dsiplays these properties:

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
   String fileName = "/samp.properties";
   String fullName = application.getRealPath(fileName);
   FileInputStream fis = null;
   try {
     fis = new FileInputStream(fullName);
   } catch (Exception e) {
     out.println(fileName + " Not Found");
   Properties props = new Properties();
   if (fis != null){
     Enumeration enum = props.propertyNames();
     while (enum.hasMoreElements()){
       Object o = enum.nextElement();
       String key = (String)o;
       String val = props.getProperty(key);
       out.println("key = " + key + " value = " + val + "<BR>");