HttpSessionListener的用法

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>

 
 

你可能感兴趣的:(HttpSessionListener的用法)