posts - 56, comments - 54, trackbacks - 0, articles - 4
             ::  ::  :: 聯(lián)系 :: 聚合  :: 管理

          HttpSessionListener的用法

          Posted on 2005-12-05 10:28 Terry的Blog 閱讀(6494) 評(píng)論(3)  編輯  收藏 所屬分類: java語(yǔ)言
          HttpSessionListener的用法
           
          參考 http://www.javaroad.jp/servletjsp/sj_servlet9.htm

          繼承HttpSessionListener接口的類,來(lái)監(jiān)聽(tīng)Session創(chuàng)建和銷毀的事件
          package jp.co.sysmex.sps.util;

          import javax.servlet.*;
          import javax.servlet.http.*;
          import jp.co.sysmex.sps.app.web.WebAccountBean;

          //①HttpSessionListener 接口的實(shí)現(xiàn)。
          public class CheckSessionServlet implements HttpSessionListener {
              private static int sesCount = 0;

              //②session生成時(shí)觸發(fā)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無(wú)效時(shí)觸發(fā)sessionDestroyed方法
              //此時(shí)session中的內(nèi)容還可以正常取道
              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>

           
           


          評(píng)論

          # re: HttpSessionListener的用法  回復(fù)  更多評(píng)論   

          2007-09-06 17:11 by
          你試驗(yàn)過(guò)嗎?不要想當(dāng)然啊!
          sessionDestroyed觸發(fā)時(shí)已經(jīng)刪除了里面的對(duì)象了

          # re: HttpSessionListener的用法  回復(fù)  更多評(píng)論   

          2007-09-09 15:24 by Terry的Blog
          以上內(nèi)容在Tomcat和Weblogic上實(shí)驗(yàn)通過(guò)。且在兩個(gè)項(xiàng)目中實(shí)際使用。
          關(guān)于“sessionDestroyed觸發(fā)時(shí)已經(jīng)刪除了里面的對(duì)象了”我也在網(wǎng)上看到過(guò)。不過(guò)不符合實(shí)驗(yàn)結(jié)果。

          # re: HttpSessionListener的用法  回復(fù)  更多評(píng)論   

          2011-10-06 18:09 by whwang
          @Terry的Blog
          正解.
          主站蜘蛛池模板: 二连浩特市| 太保市| 安西县| 黔南| 新民市| 顺昌县| 高尔夫| 清流县| 扎赉特旗| 东台市| 武宁县| 南昌县| 铜山县| 邳州市| 沁水县| 东乡| 武穴市| 武冈市| 古交市| 西城区| 蓝山县| 木兰县| 乃东县| 济源市| 皋兰县| 淄博市| 革吉县| 绥化市| 岳池县| 中西区| 利川市| 大竹县| 温泉县| 诸城市| 思茅市| 南岸区| 中江县| 灵宝市| 黑山县| 句容市| 新河县|