웹어플리케이션의 생명주기(LifeCycle)를 감시하는 리스너(Listener)가 있습니다.
그건 바로 ServletContextListener 입니다.
리스너의 해당 메소드가 웹 어플리케이션의 시작과 종료시 호출됨.
(contextInitialized(),contextDestroyed())메소드를 override 받아야함.
두가지 방법이 있습니다.
<사용방법>
1. 리스너 클래스 제작 --> web.xml파일에 리스너 클래스 기술
package com.javalec.ex;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletL implements ServletContextListener {
//웹어플리이션이 종료될때 작동 (가장 늦게 실행)
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextdestroyed");
}
//웹어플리이션이 실행될때 작동 ( 가장 먼저 실행)
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextInitialized");
}
}
리스너 클래스 제작코드 입니다.
아래 코드는 web.xml에 리스너 등록코드 입니다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.javalec.ex.ServletL</listener-class>
</listener>
</web-app>
2. 리스너 클래스 제작 --> 리스너 클래스에 리스너 등록
package com.javalec.ex;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletL implements ServletContextListener {
//웹어플리이션이 종료될때 작동 (가장 늦게 실행)
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextdestroyed");
}
//웹어플리이션이 실행될때 작동 ( 가장 먼저 실행)
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextInitialized");
}
}
'프로그래밍 > JSP' 카테고리의 다른 글
JSP 스크립트릿,선언,표현식 (0) | 2018.07.15 |
---|---|
JSP 개념 및 동작원리 (0) | 2018.07.15 |
Servlet 데이터 공유 ServletContext (0) | 2018.07.15 |
서블릿 초기화 파라미터 ServletConfig (0) | 2018.07.15 |
Tomcat 서버의 한글처리 (0) | 2018.07.15 |