issue in property file
Posted
by devuser
on Stack Overflow
See other posts from Stack Overflow
or by devuser
Published on 2010-01-13T11:00:09Z
Indexed on
2010/03/29
2:03 UTC
Read the original article
Hit count: 459
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"));
}
}
© Stack Overflow or respective owner