HttpSessionListener를 구현한 클래스를 만든다.
package test;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionMgmtListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("SessionCreated()");
HttpSession session = arg0.getSession();
System.out.println(session.getId());
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("SessionDestroyed()");
HttpSession session = arg0.getSession();
System.out.println(session.getId());
}
}
web.xml에 리스너를 등록한다.
<listener>
<listener-class>test.SessionMgmtListener</listener-class>
</listener>
<!-- 테스트를 위해 세션 만료시간을 1분으로 정한다. -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
sessionDestroyed() 메소드는 세션 종료시 자동 호출 된다.
설정한 세션 만료시간이 대던가
임의로 session.invalidate() 호출시에도 호출되니
로그 아웃 처리는 sessionDestroyed() 일괄적으로 해주는 것이 좋을듯 하다.
session이 생성 될때, 소멸 될때 호출되니 잘 활용하면 좋을 것이다.
[출처] HttpSessionListener 이벤트 리스너를 이용한 세션 생성,파괴 시 로그 처리|작성자 깨굴
------------------------------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Login Form</TITLE>
<BODY>
<FORM METHOD="GET" ACTION="login">
<h1>Login Form</h1>
<table border="1" cellpadding="5" cellspacing="0" width="600" bordercolordark="white" bordercolorlight="black">
<tr>
<td width="200">
<p align="right">사용자 아이디</td>
<td width="400"><INPUT TYPE="TEXT" NAME="userID"></td>
</tr>
<tr>
<td width="200"><p align="right">비밀번호</td>
<td width="400"><p><INPUT TYPE="PASSWORD" NAME="passwd"></td>
</tr>
<tr>
<td width="200"><p> </p></td>
<td width="400"><INPUT TYPE="SUBMIT"> <INPUT TYPE="RESET"></td>
</tr>
</table>
</FORM>
</BODY>
</HTML>
---------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>test.web.SessionListener</listener-class>
</listener>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>test.web.LoginForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
---------------------------------
package test.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LoginForm extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
request.setCharacterEncoding( "EUC-KR" );
String id = request.getParameter("userID");
String pw = request.getParameter("passwd");
HttpSession session = request.getSession();
if(session.isNew()){
session.setAttribute("login", id);
}
out.println("<html><body>");
out.println("userID는 " + id + "<br>");
out.println("password는 "+pw + "<br>");
out.println("총 접속자수는"+ SessionListener.activeSessions);
out.println("</body></html>");
}
}
----------------------------
package test.web;
import javax.servlet.http.*;
public class SessionListener implements HttpSessionListener{
static int activeSessions;
public void sessionCreated(HttpSessionEvent event){
System.out.println("sessionCreated");
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent event){
System.out.println("sessionDestroyed");
activeSessions--;
}
}//end class
'Java > Servlet' 카테고리의 다른 글
[Servlet] Tomcat spec (0) | 2012.01.09 |
---|---|
[Servlet] 톰캣 start 할때 클래스 기동하기 (0) | 2011.06.21 |
[Servlet] context 속성값 세팅하여 사용 (0) | 2011.01.28 |
[Servlet] Listener - ServletContextListener (0) | 2011.01.28 |
[Servlet] Listener (0) | 2011.01.28 |