민서네집

ServletContextListener 등록하기. 본문

Java

ServletContextListener 등록하기.

브라이언7 2013. 3. 27. 10:43

ServletContextListener 를 어떻게 등록하나?


log4j.properties 파일에서 System Property 를 참조하도록 설정을 해놓았는데, 따라서 log4j.properties 파일을 읽기 전에 System Property를 설정해야 할 필요가 있었다.


[참고] http://stackoverflow.com/questions/216781/log4j-configuring-a-web-app-to-use-a-relative-path 


Servlet 의 init() 메서드 안에서 System Property를 설정하는 것은 log4j.properties 파일을 읽고 난 다음이다.

따라서 다음과 같이 ServletContextListener 를 상속받은 클래스를 web.xml 에서 listener 로 등록해주면 된다.


만약 이렇게 해도 log4j.properties 파일을 먼저 읽게 된다면, log4j.properties 파일의 이름을 바꿔야 한다.

default 이름을 쓰지 말고, 이름을 바꿔서  web.xml 에서 Spring에서 제공해주는 Log4jConfigListener를 등록해줘야 한다.


<context-param> 
<param-name>log4jConfigLocation</param-name> 
<param-value>/WEB-INF/config/log4j.properties</param-value> 
</context-param> 
<listener> 
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 


< ServletContextListener를 web.xml에 등록하는 방법 >


http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/


<web-app ...>

<listener>

<listener-class>com.example.web.AppServletContextListener</listener-class>

</listener>

</web-app>



Comments