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中的内容还可以正常取道-此处有争议,有人认为“sessionDestroyed触发时已经删除了里面的对象了”, 不过本文中的例子在Tomcat和Weblogic上实验通过。且在两个项目中实际使用。

    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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值