728x90
✅ Listener
- Servlet 컨테이너가 수행한 특정 타입의 이벤트를 감지하여, 해당 이벤트 발생 시에 어떠한 작업을 수행하는 객체
🐥 ServletContextListener, ServletContextAttributeListener
- ServletContextListener : ServletContextEvent가 발생할 경우
- ServletContextAttributeListener : ServletContextAttributeEvent가 발생할 경우
@Slf4j
public class ContextListener implements ServletContextListener, ServletContextAttributeListener {
public ContextListener() {
}
@Override
public void contextInitialized(ServletContextEvent sce){
// 애플리케이션 실행시킬 때 실행
log.info("Context initialized !!!");
}
@Override
public void contextDestroyed(ServletContextEvent sce){
// 애플리케이션이 정상 종료될 때 실행
log.info("Context destroyed !!!");
}
@Override
public void attributeAdded(ServletContextAttributeEvent scae){
// servletContext.setAttribute() 메서드를 통해 새로운 속성이 추가될 때 실행
log.info("Context attribute added !!!");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae){
// servletContext.setAttribute() 메서드를 통해 기존 속성의 value가 새로운 값으로 덮어씌워질 때 실행
log.info("Context attribute replaced !!!");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
// servletContext.removeAttribute() 메서드를 통해 어떠한 속성이 삭제될 때 실행
System.out.println("context attribute removed !!!");
}
}
🐥 SessionListener
- HttpSessionListener : HttpSessionEvent가 발생할 경우
- HttpSessionAttributeListener : HttpSessionBindingEvent가 발생할 경우
@Slf4j
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
public SessionListener(){}
@Override
public void sessionCreated(HttpSessionEvent se) {
// 세션이 최초로 생성될 때 실행
// 이미 세션이 생성되었다면 호출되지 않음
log.info("HttpSession이 생성되었음");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// 세션이 만료될 때 실행
log.info("HttpSession이 만료되었음");
}
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// session.setAttribute() 메서드를 통해 새로운 속성이 추가될 때 실행
log.info("HttpSession 속성이 추가되었음");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
// session.removeAttribute() 메서드를 통해 어떠한 속성이 삭제될 때 실행
log.info("HttpSession 속성이 삭제되었음");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
// session.setAttribute() 메서드를 통해 기존 속성의 value가 새로운 값으로 덮어씌워질 때 실행
log.info("HttpSession 속성이 변경되었음");
}
}
🐥 RequestListener
- ServletRequestListener : ServletRequestEvent가 발생할 경우
- ServletRequestAttributeListener : ServletRequestAttributeEvent가 발생할 경우
@Slf4j
public class RequestListener implements ServletRequestListener, ServletRequestAttributeListener {
public RequestListener(){}
@Override
public void requestInitialized(ServletRequestEvent sre) {
// servlet request가 생성될 때 실행
log.info("request initialized !!!");
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
// servlet service 실행되고 브라우저에게 응답을 완료할 때 실행
log.info("request destroyed !!!");
}
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
// request.setAttribute() 메서드를 통해 새로운 속성이 추가될 때 실행
log.info("request attribute added !!!");
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
// request.removeAttribute() 메서드를 통해 어떠한 속성이 삭제될 때 실행
log.info("request attribute removed !!!");
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
// request.setAttribute() 메서드를 통해 기존 속성의 value가 새로운 값으로 덮어씌워질 때 실행
log.info("request attribute replaced !!!");
}
}
✅ References
- https://forsaken.tistory.com/m/entry/Servlet-JSP-%EC%84%9C%EB%B8%94%EB%A6%BF-listener
728x90
'개인 공부 > WEB-Servlet,JSP' 카테고리의 다른 글
Tomcat에서 초간단 프로그램 돌리기 (java, jsp) (0) | 2024.07.26 |
---|---|
Tomcat 설치하고 실행시키기 (macOS, tomcat@9, jdk11) (0) | 2024.07.25 |
[jsp] jsp 기본 문법 (4) | 2024.01.08 |
WAR (2) | 2024.01.04 |
[servlet] setCharacterEncoding, setContentType 차이 (3) | 2024.01.04 |