如何在JSP中创建servlet来修改属性文件中的键值对?

时间:2016-04-12 06:55:32

标签: java jsp properties-file

我想为我的应用程序开发一个servlet,以便我能够在运行时从属性文件中更改配置属性,例如通过HTML中的提示。真的需要它。

2 个答案:

答案 0 :(得分:0)

我的申请中有类似的要求。这就是我们所做的。

a)在应用程序启动时,属性文件的内容将加载到应用程序范围的映射变量中。这可以通过使用ServletContextListener,Filter或Servlet(init方法)

来完成

b)您必须确保应用程序使用地图而不是属性文件直接获取键值对。

c)更新属性文件时。你必须重新加载应用程序范围的地图。

答案 1 :(得分:0)

您需要在您的网络应用中使用工具ServletContextListener,请参见下面的示例....

public class MyAppServletContextListener 
               implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        //close stream or connections that you created to read from property file
    }

    //Run this before web application is started
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // Add your property reading code here..    
    }
}

一旦你这样做,你需要在web.xml se中输入...

<web-app ...>
   <listener>
    <listener-class>
             com.yourpackagestructure.MyAppServletContextListener 
        </listener-class>
   </listener>
</web-app>

一旦从MyAppServletContextListener初始化了属性,就可以从那里读取它。