HttpSessionListener的用法
參考 http://www.javaroad.jp/servletjsp/sj_servlet9.htm
繼承HttpSessionListener接口的類,來監聽Session創建和銷毀的事件
package jp.co.sysmex.sps.util;
import javax.servlet.*;
import javax.servlet.http.*;
import jp.co.sysmex.sps.app.web.WebAccountBean;
//①HttpSessionListener 接口的實現。
public class CheckSessionServlet implements HttpSessionListener {
private static int sesCount = 0;
//②session生成時觸發sessionCreated方法
public void sessionCreated(HttpSessionEvent hse) {
sesCount++;
//ServletContext sc = hse.getSession().getServletContext();
String sessid = hse.getSession().getId();
System.out.println(" session Created " + sesCount);
System.out.println(" session ++ " + sessid);
}
//③session無效時觸發sessionDestroyed方法
//此時session中的內容還可以正常取道
public void sessionDestroyed(HttpSessionEvent hse) {
String sessid = hse.getSession().getId();
System.out.println(" session Destroyed " + sesCount);
System.out.println(" session -- " + sessid);
WebAccountBean account = (WebAccountBean)(hse.getSession().getAttribute("ACCOUNT_KEY"));
System.out.println(account.getEnterpriseCode());
System.out.println(account.getEnterpriseFullKanjiName());
sesCount--;
}
}
web.xml文件中增加配置信息.
<listener>
<listener-class>jp.co.sysmex.sps.util.CheckSessionServlet</listener-class>
</listener>
參考 http://www.javaroad.jp/servletjsp/sj_servlet9.htm
繼承HttpSessionListener接口的類,來監聽Session創建和銷毀的事件
package jp.co.sysmex.sps.util;
import javax.servlet.*;
import javax.servlet.http.*;
import jp.co.sysmex.sps.app.web.WebAccountBean;
//①HttpSessionListener 接口的實現。
public class CheckSessionServlet implements HttpSessionListener {
private static int sesCount = 0;
//②session生成時觸發sessionCreated方法
public void sessionCreated(HttpSessionEvent hse) {
sesCount++;
//ServletContext sc = hse.getSession().getServletContext();
String sessid = hse.getSession().getId();
System.out.println(" session Created " + sesCount);
System.out.println(" session ++ " + sessid);
}
//③session無效時觸發sessionDestroyed方法
//此時session中的內容還可以正常取道
public void sessionDestroyed(HttpSessionEvent hse) {
String sessid = hse.getSession().getId();
System.out.println(" session Destroyed " + sesCount);
System.out.println(" session -- " + sessid);
WebAccountBean account = (WebAccountBean)(hse.getSession().getAttribute("ACCOUNT_KEY"));
System.out.println(account.getEnterpriseCode());
System.out.println(account.getEnterpriseFullKanjiName());
sesCount--;
}
}
web.xml文件中增加配置信息.
<listener>
<listener-class>jp.co.sysmex.sps.util.CheckSessionServlet</listener-class>
</listener>