Posted on 2005-12-05 10:28
Terry的Blog 阅读(6479)
评论(3) 编辑 收藏 所属分类:
java语言
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>