2010-01-13 59 views
0

我想加载属性文件,当tomcat start.so我使用servletContextListener来做到这一点,我可以得到属性文件的值到我的Web应用程序。 但是我想在改变属性文件一次登录到web应用程序后保持相同的值。但是当我更改属性文件的值并再次登录到系统时,它将值更改为新的值。我想保留相同的值当tomcat正在启动时加载,我可以实现这个吗?问题属性文件

我的编码是如下

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")); 
    } 
} 
+1

你确信你的情况下没有被重新加载? – Bozho 2010-01-13 11:10:23

+0

当注销并记录到system.context未重新加载时,属性文件将再次加载。 – devuser 2010-01-13 11:37:06

回答

0

我怀疑你的上下文初始化被调用一次因为某些原因更多。您是否多次看到此打印语句“Application X正在启动”?如果这是被多次调用,那么确保你的属性不加载新数据的方法是确保它们只加载一次......查看代码。

.... 
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()); 
    } 
}  

通过使用空状态来检查它是否以前已加载,您可以确保它只加载一次。当然,你也可以使用布尔值或其他指标。

+0

我已经尝试了上面的code.but上下文重新加载时,我从系统注销并更改属性文件,并再次腰部系统。 – devuser 2010-01-15 05:04:18

+0

上面的代码应该防止重新加载属性,即使上下文重新加载,是不是工作?如果不是,我不太确定发生了什么...属性是静态定义的,应该只根据if(props == null)行加载一次。 – PaulP1975 2010-01-17 00:40:44

+0

我已经尝试了上面提到的代码,但仍然出现同样的问题。当服务器启动时“Application X正在启动”打印一次,然后我注销并更改属性值并再次登录到系统。然后“应用程序X正在启动“再次打印并且系统中的属性值已更改。 – devuser 2010-01-15 03:50:15

0

我一直在尝试做同样的事情。

我最终做的是在应用程序的文件夹之外重新创建属性文件,然后读取web服务中的新文件。

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

}

,然后在上下文监听器:

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