1

I am currently using a ServletContextListener to set the paths of JSPs in a web application. The paths are stored as context parameters in web.xml and retrieved by the listener:

    @Override
    public void contextInitialized(ServletContextEvent sce) {        
        ServletContext sc = sce.getServletContext();                           
        sc.setAttribute("urlOfThisPage", sc.getInitParameter("urlOfThisPage"));   
        sc.setAttribute("urlOfThatPage", sc.getInitParameter("urlOfThatPage"));    

In the application servlets, the path of a particular JSP can easily be retrieved from ServletContext.

My question relates to handling a properties file in the same way. I read a lot about this on other StackOverflow pages like 2161045.

Am I correct in assuming that the properties file should be read by a listener and stored in ServletContext using a Property object? but then if this is the case, how would I retrieve a particular property from the properties file?

At the moment I am using this sort of code in my servlets to get the value of an attribute from ServletContext.

String url = (String) sc.getAttribute("urlOfThisPage");  // Use ServletContext to get JSP's URL.    

But I am not sure how to extend this to accessing a properties file.

I have tried the following in the ServletContextListener:

    Properties properties = new Properties();
    properties.setProperty("name", "Akechi Jinsai");
    sc.setAttribute("properties", properties);

And in a servlet, using code:

   ServletContext sc = request.getSession().getServletContext();        
   Properties properties = (Properties) sc.getAttribute("properties");
   System.out.println("Here: " + properties.getProperty("name"));

"Here: Akechi Jinsai" is displayed but is there a better way of getting a single property in a servlet without looking up things in this way?

Community
  • 1
  • 1
Mr Morgan
  • 1,965
  • 13
  • 44
  • 73
  • "Am I correct in assuming that the properties file should be read by a listener" - no. – Nir Alfasi May 30 '14 at 23:34
  • Then where in a web application would be the best place to read the properties file and store it? – Mr Morgan May 30 '14 at 23:37
  • The properties file should be read during startup and kept in memory. It may be stored in a utility class (could be singleton) as a static field, for example. – Nir Alfasi May 30 '14 at 23:39
  • Or through the init method of a servlet I suppose? – Mr Morgan May 30 '14 at 23:44
  • You can use both `getServletContext().getResourceAsStream(...)` (if the file is in the classpath) as well as use absolute path - but I don't see any connection to that fact with a property file "should be read by a listener". A listener listens to events and runs callbacks on the occurrence of those events - it has nothing to do with loading a properties file. – Nir Alfasi May 30 '14 at 23:49
  • True, there is no absolute need to use a listener but I am thinking of using a listener of the right type, i.e. a ServletContextListener, to set the properties up when the ServletContext starts. Or to call a class that can do it. I have revised the question to show this. – Mr Morgan May 30 '14 at 23:53

1 Answers1

2

Simply load the properties file in Servlet and move the values into HashMap and store it as application attribute. Now access it in JSP using JavaServer Pages Standard Tag Library.

Read more about Load properties file in Servlet/JSP.


Sample code:

JSP: (Different ways to access the map)

<c:forEach items="${map}" var="entry">
    Key="${entry.key}" Value=${entry.value}
</c:forEach>

URL Of This Page = ${map.urlOfThisPage }
URL Of That Page = ${map.urlOfThatPage }


URL Of This Page = ${map['urlOfThisPage'] }
URL Of That Page = ${map['urlOfThatPage'] }

ServletContextListener

public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent sc) {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc = sce.getServletContext();
        // load the properties file if needed
        // read the path from web.xml as init parameter 

        Map<String, String> map = new HashMap<String, String>();
        map.put("urlOfThisPage", sc.getInitParameter("urlOfThisPage"));
        map.put("urlOfThatPage", sc.getInitParameter("urlOfThatPage"));

        sc.setAttribute("map", map);
    }

}

web.xml:

<context-param>
    <param-name>urlOfThisPage</param-name>
    <param-value>url</param-value>
</context-param>
<context-param>
    <param-name>urlOfThatPage</param-name>
    <param-value>url</param-value>
</context-param>

<listener>
    <listener-class>com.x.y.z.MyServletContextListener</listener-class>
</listener>
Community
  • 1
  • 1
Braj
  • 44,339
  • 5
  • 51
  • 69
  • I would use a properties object instead of map, but the basic principle is the same. – Mr Morgan May 30 '14 at 23:58
  • Properties extends `Hashtable`. Yes the basic concept is same. – Braj May 31 '14 at 00:00
  • It's along the lines of what I want anyway. – Mr Morgan May 31 '14 at 00:01
  • Should I code for you using properties file as well or you can do it by simple using `Hashtable` instead of `HashMap` in my code. – Braj May 31 '14 at 00:02
  • If I'm right all I need to do is load the properties using something like `properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties"))`. I am assuming that after this the properties would be loaded into an attribute in `ServletContext` and then in a servlet, a particular property would be looked up as my code sample above in the question demonstrates? Or is there a better way to get a property in a servlet? – Mr Morgan May 31 '14 at 00:07
  • First read the path from web.xml as I suggested rather than hard coding it in listener class itself. – Braj May 31 '14 at 00:08
  • Of course, I just copied the code from another page. – Mr Morgan May 31 '14 at 00:09
  • That's OK what about JSP? How to access the property in JSP page? – Braj May 31 '14 at 00:10
  • I am trying to avoid using any properties in the JSPs. All the paths to JSPs are stored in `web.xml` anyway and read by the listener and stored in `ServletContext` attributes. So that's why I thought to use an attribute in `ServletContext` for the properties. The JSP paths could become properties as well. But is storing the properties as an attribute in `ServletContext` the best way of doing things – Mr Morgan May 31 '14 at 00:12
  • You can simply create a static member of type Properties in ServletContextListner class itself and access it statically. There is no need to put in the context at all. – Braj May 31 '14 at 00:14
  • 1
    A static instance of Property? Good idea. I never thought of that. – Mr Morgan May 31 '14 at 00:15
  • It will be always available to the application because it's initialized at the application start-up. – Braj May 31 '14 at 00:16
  • Yes. and only ever be read once upon startup. I like it!-) Many thanks Braj. – Mr Morgan May 31 '14 at 00:17
  • Works very well. Thanks again Braj. – Mr Morgan May 31 '14 at 20:12