Listener : 감시자 역할
@WebListener
public class MySessionListener implements HttpSessionAttributeListener {
private int loginedCnt;
public MySessionListener() {
// 객체 생성되고 생성자 호출될 때 출력 될 것
System.out.println("MySessionListener객체 생성됨");
}
public void attributeAdded(HttpSessionBindingEvent se) {
// 객체 생성되고 생성자 호출될 때 출력 될 것
// System.out.println("MySessionListener.attributeAdded()메서드 호출됨");
if(se.getName().equals("loginedId")) {
loginedCnt++;
System.out.println("로그인되었습니다: 로그인된 총사용자 수 :" + loginedCnt);
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
// System.out.println("MySessionListener.attributeRemoved()메서드 호출됨");
if(se.getName().equals("loginedId")) {
loginedCnt--;
System.out.println("로그아웃되었습니다: 로그인된 총사용자 수 :" + loginedCnt);
}
}
public void attributeReplaced(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
}
}
다른 Listener 생성
@WebListener
public class MyServletContextListener implements ServletContextListener {
public MyServletContextListener() {
// TODO Auto-generated constructor stub
}
public void contextDestroyed(ServletContextEvent sce) {
// 프로젝트가 끝날 때 정리작업하는 메서드
}
public void contextInitialized(ServletContextEvent sce) {
// tomcat이 켜지자마자 web프로젝트에서 필요한 준비작업을 코딩해주면 됨
// DB와의 작업을 미리 만들어둔다던가. db connection pool과 같은 준비작업
// response.setHeader("Access-Control-Allow-Origin", "http://192.168.1.19:5500");
// response.setHeader("Access-Control-Allow-Credentials", "true"); 이런거 선언해도됨
System.out.println("MyServletContextListener.contextInitialized()호출됨");
}
}
'프레임워크 > Spring' 카테고리의 다른 글
라이브러리 dependency로 추가하고 setting 하기 (0) | 2023.10.24 |
---|---|
Maven Project 생성 (0) | 2023.10.24 |
web module version (0) | 2023.10.24 |
Filter (0) | 2023.10.24 |
리플랙션(Reflection) (0) | 2023.10.23 |