2010-01-13 3 views
0

에서 문제가 발생했습니다. tomcat이 시작될 때 속성 파일을로드하려고합니다. servletContextListener를 사용하여 웹 응용 프로그램에 속성 파일 값을 가져올 수 있습니다. 하지만 한 번 웹 응용 프로그램에 로그인 할 때 속성 파일을 변경 한 후 동일한 값을 유지하고 싶습니다. 그러나 속성 파일의 값을 변경하고 시스템에 다시 로그인하면 새로운 값으로 변경됩니다. 동일한 값을 유지하려고합니다. 바람둥이 때 시작했다 .how 내가 이것을 구현할 수 있습니까?속성 파일

내 코딩은 내가 당신의 상황에 맞는 초기화가 한번 어떤 이유로 이상 호출되고 있음을 의심

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

가 확실 만든 사용자의 컨텍스트가 다시로드되지? 로그 아웃하고 system.context로 로깅이 다시로드되지 않으면 – Bozho

+0

등록 정보 파일이 다시로드됩니다. – devuser

답변

0

다음과 같다. 이 인쇄 문을 두 번 이상 "응용 프로그램 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()); 
    } 
}  

null 상태를 사용하여 이전에로드되었는지 확인하면로드가 한 번만 수행되도록 할 수 있습니다. 물론 부울 또는 다른 표시기를 사용할 수도 있습니다.

+0

위의 코드를 시도해 보았습니다. 그러나 시스템에서 로그 아웃하고 속성 파일을 변경하고 다시 시스템에 로그인하면 컨텍스트가 다시로드됩니다. – devuser

+0

위의 코드는 컨텍스트가 다시로드되는 경우에도 속성이 다시로드되지 않도록해야합니다. 그렇지 않다면, 나는 어떤 일이 일어나고 있는지 잘 모르겠다 ... 속성은 정적으로 정의되고 if (props == null) 행을 기반으로 한 번만로드되어야한다. – PaulP1975

+0

나는 위에서 언급 한 코드를 시도했지만 여전히 동일한 문제가 발생합니다. 서버가 "Application X is starting"을 시작하면 인쇄가 한 번 수행 된 후 로그 아웃되고 속성 값이 변경되고 시스템에 다시 로그인됩니다. "Application X가 시작됩니다 "라는 메시지가 다시 인쇄되고 속성 값이 변경되었습니다. – devuser

0

나는 똑같은 일을 시도해 왔습니다.

내가 끝낸 것은 응용 프로그램의 폴더 외부에서 속성 파일을 다시 만든 다음 웹 서비스에서 새 파일을 읽는 것입니다.

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);