简体   繁体   中英

Reading properties file in Java web application

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM