views:

128

answers:

2

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"));   
    }
}
A: 

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.

PaulP1975
I have tried the above code.but context is reloading when i log out from the system and change the property file and again loin to the system.
devuser
the code above should prevent the properties from reloading even if the context is reloading, is that not working? if not, i am not quite sure what is going on... the properties are statically defined and should only be loaded once based on the if (props == null) line.
PaulP1975
A: 

i have tried the code you mentioned above but still the same problem occurs.When server is starting "Application X is starting" prints once and after that i log out and change the property value and log in to the system again.Then "Application X is starting" prints again and value of property has changed in the system.

devuser