简体   繁体   中英

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. 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 . This can be done by using a ServletContextListener, Filter or a Servlet (init method)

b) You have to be sure the application fetches the key value pairs using the map and not the property file directly.

c) When you update the property file. you have to just reload the Application scoped Map.

You Need implement ServletContextListener in your web application, see below example....

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

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