posts - 27,  comments - 14,  trackbacks - 0
          要有這么一個(gè)監(jiān)聽(tīng)器,當(dāng)加入session時(shí)就可以觸發(fā)一個(gè)加入session事件,在session過(guò)期時(shí)就可以觸發(fā)一個(gè)刪除事件,那么我們的把要處理的東西加入到這兩個(gè)事件中就可以做很多于SESSION相關(guān)連的事。如在線用戶(hù)的管理,單點(diǎn)登陸等等。
          在J2EE中可以實(shí)現(xiàn)HttpSessionBindingListener接口,此接口有兩要實(shí)現(xiàn)的方法。
           void valueBound(HttpSessionBindingEvent event) 當(dāng)實(shí)現(xiàn)此接口的監(jiān)聽(tīng)類(lèi)和session綁定時(shí)觸發(fā)此事件。
          void valueUnbound(HttpSessionBindingEvent event) 當(dāng)session過(guò)期或?qū)崿F(xiàn)此接口的監(jiān)聽(tīng)類(lèi)卸裁時(shí)觸發(fā)此事件。

          下面是一個(gè)示例解決方案:可以把登陸用戶(hù)的信息記錄在緩沖池中,當(dāng)SESSION過(guò)期時(shí),用戶(hù)信息自動(dòng)刪除。

          一個(gè)用戶(hù)信息接口:
          public interface LoginUserMessage {}

          一個(gè)用戶(hù)緩沖池:
          public class LoginUserPool {
              
          private Map map = new HashMap();
              
          private static LoginUserPool loginUserPool = new LoginUserPool();
              
          private LoginUserPool(){}
              
          public static LoginUserPool getInstance() {
                  
          return loginUserPool;
              }

              
          public void addLoginUserMessage(String sessionId,LoginUserMessage loginUserMessage){
                 map.remove(sessionId);
                 map.put(sessionId,loginUserMessage);
              }

              
          public LoginUserMessage removeLoginUserMessage(String sessionId){
                  
          return  (LoginUserMessage) map.remove(sessionId);
              }

              
          public LoginUserMessage getLoginUserMessage(String sessionId){
                  
          return (LoginUserMessage) map.get(sessionId);
              }

              
          public Map getLoginUserMessages(){
                  
          return map;
              }

              
          public boolean isEmpty(){
                  
          return  map.isEmpty();
              }

          }


          一個(gè)HttpSessionBindingListener接口的監(jiān)聽(tīng)類(lèi):
          public class UserLoginListener implements HttpSessionBindingListener{
              
          private final Log logger = LogFactory.getLog(getClass());
              
          private String sessionId = null;
              
          private LoginUserMessage loginUserMessage = null;
              
          private LoginUserPool loginUserPool = LoginUserPool.getInstance();

              
          public LoginUserMessage getLoginUserMessage() {
                  
          return loginUserMessage;
              }

              
          public void setLoginUserMessage(LoginUserMessage loginUserMessage) {
                  
          this.loginUserMessage = loginUserMessage;
              }

              
          public String getSessionId() {
                  
          return sessionId;
              }

              
          public void setSessionId(String sessionId) {
                  
          this.sessionId = sessionId;
              }

              
          /**//* (non-Javadoc)
               * 
          @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
               
          */

              
          public void valueBound(HttpSessionBindingEvent event) {
                  
          // TODO Auto-generated method stub
                  if(this.getLoginUserMessage() != null){
                      loginUserPool.addLoginUserMessage(
          this.getSessionId(),this.getLoginUserMessage());
                      logger.info(
          "用戶(hù)信息加入緩存池成功");
                  }

                  
          this.setLoginUserMessage(null);
              }


              
          /**//* (non-Javadoc)
               * 
          @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
               
          */

              
          public void valueUnbound(HttpSessionBindingEvent event) {
                  
          // TODO Auto-generated method stub
                  if(!loginUserPool.isEmpty()){
                      loginUserPool.removeLoginUserMessage(sessionId);
                      logger.info(
          "用戶(hù)信息從緩存池中移除成功");
                  }

              }


          }

          這樣子的話,當(dāng)在應(yīng)用中把userLoginListener加入到session中時(shí),就會(huì)自動(dòng)把用戶(hù)信息加入到緩沖池中了。
          如:
           session.setAttribute("userLoginListener",userLoginListener);



          (原創(chuàng),轉(zhuǎn)載請(qǐng)保留文章出處http://www.aygfsteel.com/bnlovebn/archive/2007/07/04/128006.html

          posted on 2007-07-12 17:11 Scott.Pan 閱讀(406) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 代碼收藏夾
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(4)

          隨筆分類(lèi)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 新平| 西乌| 常州市| 织金县| 庆城县| 交口县| 双桥区| 哈巴河县| 青阳县| 石嘴山市| 汤原县| 绩溪县| 肃宁县| 德令哈市| 黎平县| 盖州市| 九江市| 阿城市| 连山| 桃江县| 江都市| 永平县| 九江市| 毕节市| 金山区| 沙坪坝区| 黔东| 黄山市| 剑河县| 红桥区| 贵州省| 随州市| 嘉峪关市| 于都县| 东至县| 福建省| 抚远县| 拉孜县| 金坛市| 清苑县| 绥芬河市|