简体   繁体   English

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

[英]How do i create a servlet in JSP to modify the key value pairs in a property file?

I want to develop a servlet for my application so that i am able to change the configuration properties at the run time from the properties file.. for example through a prompt in HTML. 我想为我的应用程序开发一个servlet,以便能够在运行时从属性文件中更改配置属性。例如,通过HTML中的提示。 Really need it. 真的需要它。

I have a similar requirement in my application. 我的申请中有类似的要求。 This is what we have done. 这就是我们所做的。

a) The content of the property file are loaded into a application scoped map variable at the time of Application start up . a)在应用程序启动时,属性文件的内容被加载到应用程序范围的映射变量中。 This can be done by using a ServletContextListener, Filter or a Servlet (init method) 这可以通过使用ServletContextListener,Filter或Servlet(init方法)来完成。

b) You have to be sure the application fetches the key value pairs using the map and not the property file directly. b)您必须确保应用程序使用映射而非属性文件直接获取键值对。

c) When you update the property file. c)更新属性文件时。 you have to just reload the Application scoped Map. 您只需要重新加载应用程序范围的地图即可。

You Need implement ServletContextListener in your web application, see below example.... 您需要在您的Web应用程序中实现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..    
    }
}

once you do this you need to make entry in web.xml se following... 完成此操作后,您需要在以下网站中输入web.xml文件:

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

once you have initialised your property from MyAppServletContextListener then you can read it from there.. 一旦从MyAppServletContextListener初始化了属性,就可以从那里读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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