开发者

issue in property file

I want to load the property file when tomcat is starting.so I'm using servletContextListener to do that and i can get values of property file to my web application. But i want to keep the same value after changing the property file once log into web application.But when i change the value of property file and log into system again it change the value to new one.I want to keep the same value that loaded when tomcat was starting.how can i implement this?

My coding is as below

import javax.servlet.*;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.util.ResourceBundle;


public final class sysProperties  implements javax.servlet.ServletContextListener {   
   private static Properties props = new Properties();
   private static String file_name = "com/util/contact.properties";

   public addSystemProperties() {
   }

   public void contextInitialized(ServletContextEvent servletContextEvent) {

        // Get the context
        ServletContext servletContext = servletContextEvent.getServletContext();

        // Set a context attribute
        try {
            // props.load(servletContext.getResourceAsStream(file_name));
            props.load(getClass().getClassLoader().getResourceAsStream(file_name));

            System.out.println(" Application X is starting");

            servletContext.setAttribute("h1",props.getProperty("home.h1"));
            servletContext.setAttribute("h2",props.getProperty("home.h2"));

            System.out.println("h1"+servletContext.getAttribute("h1"));
            System.out.println("h2"+ servletContext.getAttribute("h2"));
            ;

        } catch (Exception e) {
            System.out.println(" Error setting context attribute: " + e.getMessage());
        }
    }

   public void contextDestroyed(ServletContextEvent servletContextEvent) {

        // Get the context
        ServletContext servletContext = servletContextEvent.getServletContext();

        // Output the context variable we set earlier
        System.out.println(" Application X is shutting down");
        System.out.println(" Value of h1 is: " + servletContext.getAttribute("h1"));
        System.out.println(" Value of h2 is: " + servletContext.getAttribute("h2"));

        // Clean up (not really necessary as the context is being destroyed, but let's be neat)
        servletContext.removeAttribute(props.开发者_运维百科getProperty("h1"));
        servletContext.removeAttribute(props.getProperty("h2"));   
    }
}


I suspect that your context initialization is being called more than once for some reason. Do you see this print statement more than once "Application X is starting"? If this is being called multiple times then the way to make sure your properties don't load new data is to ensure they are loaded only once... see code.

....
private static Properties props = null;
....
public void contextInitialized(ServletContextEvent servletContextEvent) {

    // Get the context
    ServletContext servletContext = servletContextEvent.getServletContext();

    // Set a context attribute
    try {
        // props.load(servletContext.getResourceAsStream(file_name));
        if (props == null)
        {
            props = new Properties();
            props.load(getClass().getClassLoader().getResourceAsStream(file_name));
        }
        System.out.println(" Application X is starting");

        servletContext.setAttribute("h1",props.getProperty("home.h1"));
        servletContext.setAttribute("h2",props.getProperty("home.h2"));

        System.out.println("h1"+servletContext.getAttribute("h1"));
        System.out.println("h2"+ servletContext.getAttribute("h2"));
    } catch (Exception e) {
        System.out.println(" Error setting context attribute: " + e.getMessage());
    }
}    

By using the null state to check if it has been previously loaded, you can ensure it is loaded only once. Of course, you could use a boolean or some other indicator as well.


I've been trying doing exactly the same.

What I ended up doing was re-creating the properties file OUTSIDE the folder of the application, and then, reading the new file in the webservice.

class SetKey{

PrintWriter output = null;

public void writeToFile(HashMap map, String fileName) throws Exception {
    Properties properties = new Properties();
    Set set = map.keySet();
    Iterator itr = set.iterator();
    while (itr.hasNext()) {
        String key = (String) itr.next();
        String value = (String) map.get(key);
        properties.setProperty(key, value);
    }
    properties.store(new FileOutputStream(fileName), "");
}

}

And then, in the context listener:

               SetKey wtp;
                wtp = new SetKey();
                HashMap map = new HashMap();
                map.put("NewKey", Value);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜