实现浏览器直接的session共享问题

来源:互联网 发布:淘宝中老年女装夏装 编辑:程序博客网 时间:2024/04/26 14:25

public class MySessionContext {

  private static HashMap mymap = new HashMap();

     public static synchronized void AddSession(HttpSession session) {
         if (session != null) {
             mymap.put(session.getId(), session);
         }
     }

     public static synchronized void DelSession(HttpSession session) {
         if (session != null) {
             mymap.remove(session.getId());
         }
     }

     public static synchronized HttpSession getSession(String session_id) {
         if (session_id == null)
         return null;
         return (HttpSession) mymap.get(session_id);
     }


}

 

第二步:

 

public class MySessionListener implements HttpSessionListener {

  public void sessionCreated(HttpSessionEvent httpSessionEvent) {
      MySessionContext.AddSession(httpSessionEvent.getSession());
      }

      public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
          HttpSession session = httpSessionEvent.getSession();
          MySessionContext.DelSession(session);
      }

 

}

第三步:

 

<listener>
  <listener-class>com.tr.MySessionListener</listener-class>
 </listener>