【问题标题】:Reading properties file in Java web application在 Java Web 应用程序中读取属性文件
【发布时间】:2014-05-30 23:27:53
【问题描述】:

我目前正在使用ServletContextListener 在 Web 应用程序中设置 JSP 的路径。路径作为上下文参数存储在web.xml 中并由侦听器检索:

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

在应用程序 servlet 中,可以从 ServletContext 轻松检索特定 JSP 的路径。

我的问题与以相同方式处理属性文件有关。我在其他 StackOverflow 页面(如 2161045)上阅读了很多关于此的内容。

我是否正确假设属性文件应该由侦听器读取并使用Property 对象存储在ServletContext 中?但如果是这种情况,我将如何从属性文件中检索特定属性?

目前,我在我的 servlet 中使用这种代码从 ServletContext 获取属性值。

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

但我不确定如何将其扩展到访问属性文件。

我在ServletContextListener中尝试了以下方法:

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

在 servlet 中,使用代码:

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

“这里:Akechi Jinsai”已显示,但有没有更好的方法来获取 servlet 中的单个属性而无需以这种方式查找内容?

【问题讨论】:

  • “我是否正确假设属性文件应该由侦听器读取” - 不。
  • 那么在 Web 应用程序中的哪个位置是读取和存储属性文件的最佳位置?
  • 属性文件应该在启动时读取并保存在内存中。例如,它可以作为静态字段存储在实用程序类(可能是单例)中。
  • 或者我想是通过 servlet 的 init 方法?
  • 您可以同时使用getServletContext().getResourceAsStream(...)(如果文件在类路径中)以及使用绝对路径 - 但我看不到与属性文件“应该读取的事实有任何联系”由听众”。侦听器侦听事件并在这些事件发生时运行回调 - 它与加载属性文件无关。

标签: java servlets properties-file servletcontextlistener


【解决方案1】:

只需在 Servlet 中加载属性文件并将值移动到 HashMap 并将其存储为应用程序属性。现在使用 JavaServer Pages Standard Tag Library 在 JSP 中访问它。

阅读更多关于Load properties file in Servlet/JSP的信息。


示例代码:

JSP:(访问地图的不同方式)

<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>

【讨论】:

  • 我会使用属性对象而不是map,但基本原理是一样的。
  • 属性扩展Hashtable。是的,基本概念是一样的。
  • 反正跟我想要的差不多。
  • 我是否也应该使用属性文件为您编码,或者您可以通过在我的代码中使用Hashtable 而不是HashMap 来完成。
  • 属性的静态实例?好主意。我从来没想过。
猜你喜欢
  • 1970-01-01
  • 2012-08-18
  • 2011-03-10
  • 1970-01-01
  • 2013-05-25
  • 2011-05-17
  • 2013-12-22
  • 2010-10-04
  • 1970-01-01
相关资源
最近更新 更多