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

          HttpSessionListener的用法

          Posted on 2005-12-05 10:28 Terry的Blog 閱讀(6487) 評論(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
          正解.
          主站蜘蛛池模板: 岱山县| 江永县| 乌兰浩特市| 陕西省| 兖州市| 无极县| 曲阳县| 厦门市| 安达市| 广德县| 黄龙县| 井研县| 涟水县| 永德县| 建平县| 阿合奇县| 新丰县| 沈阳市| 张家界市| 和硕县| 武平县| 淳化县| 苏尼特左旗| 石嘴山市| 杭锦后旗| 巢湖市| 德兴市| 高州市| 体育| 民和| 延庆县| 乌兰浩特市| 宜兴市| 黄浦区| 法库县| 晋城| 马龙县| 汝城县| 太仓市| 香港| 耿马|