posts - 56, comments - 54, trackbacks - 0, articles - 4
   ::  ::  :: 联系 :: 聚合  :: 管理

HttpSessionListener的用法

Posted on 2005-12-05 10:28 Terry的Blog 阅读(6469) 评论(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>

 
 


评论

# re: HttpSessionListener的用法  回复  更多评论   

2007-09-06 17:11 by
你试验过吗?不要想当然啊!
sessionDestroyed触发时已经删除了里面的对象了

# re: HttpSessionListener的用法  回复  更多评论   

2007-09-09 15:24 by Terry的Blog
以上内容在Tomcat和Weblogic上实验通过。且在两个项目中实际使用。
关于“sessionDestroyed触发时已经删除了里面的对象了”我也在网上看到过。不过不符合实验结果。

# re: HttpSessionListener的用法  回复  更多评论   

2011-10-06 18:09 by whwang
@Terry的Blog
正解.

只有注册用户登录后才能发表评论。


网站导航: