posts - 27,  comments - 14,  trackbacks - 0
          要有這么一個監聽器,當加入session時就可以觸發一個加入session事件,在session過期時就可以觸發一個刪除事件,那么我們的把要處理的東西加入到這兩個事件中就可以做很多于SESSION相關連的事。如在線用戶的管理,單點登陸等等。
          在J2EE中可以實現HttpSessionBindingListener接口,此接口有兩要實現的方法。
           void valueBound(HttpSessionBindingEvent event) 當實現此接口的監聽類和session綁定時觸發此事件。
          void valueUnbound(HttpSessionBindingEvent event) 當session過期或實現此接口的監聽類卸裁時觸發此事件。

          下面是一個示例解決方案:可以把登陸用戶的信息記錄在緩沖池中,當SESSION過期時,用戶信息自動刪除。

          一個用戶信息接口:
          public interface LoginUserMessage {}

          一個用戶緩沖池:
          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();
              }

          }


          一個HttpSessionBindingListener接口的監聽類:
          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(
          "用戶信息加入緩存池成功");
                  }

                  
          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(
          "用戶信息從緩存池中移除成功");
                  }

              }


          }

          這樣子的話,當在應用中把userLoginListener加入到session中時,就會自動把用戶信息加入到緩沖池中了。
          如:
           session.setAttribute("userLoginListener",userLoginListener);



          (原創,轉載請保留文章出處http://www.aygfsteel.com/bnlovebn/archive/2007/07/04/128006.html

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

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 麦盖提县| 贵州省| 惠东县| 大城县| 桑植县| 湖南省| 揭西县| 武平县| 荥经县| 孟州市| 昌图县| 米脂县| 新田县| 连南| 凤凰县| 溧水县| 东莞市| 翁牛特旗| 南溪县| 四平市| 宁阳县| 苍梧县| 门头沟区| 樟树市| 濮阳县| 府谷县| 突泉县| 土默特右旗| 彭山县| 皮山县| 聂拉木县| 广水市| 阳西县| 峨眉山市| 康定县| 廉江市| 海宁市| 安远县| 永修县| 内丘县| 石渠县|