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

          HttpSessionListener的用法

          Posted on 2005-12-05 10:28 Terry的Blog 閱讀(6494) 評論(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
          正解.
          主站蜘蛛池模板: 山阳县| 万州区| 松滋市| 广饶县| 龙南县| 泗水县| 江陵县| 邢台县| 承德县| 石河子市| 阳曲县| 南雄市| 杂多县| 四平市| 新乡市| 凤山县| 武威市| 张家界市| 泰顺县| 阳东县| 北流市| 固阳县| 侯马市| 合肥市| 阿城市| 湖北省| 靖远县| 普兰店市| 鸡泽县| 龙川县| 江津市| 武鸣县| 兖州市| 新巴尔虎左旗| 万宁市| 淮安市| 徐州市| 沙河市| 家居| 根河市| 慈利县|