使用HttpSessionListener接口監聽Session的建立和失效

轉自:http://uule.iteye.com/blog/824115java

HttpSessionListener :

   Session建立事件發生在每次一個新的session建立的時候,相似地Session失效事件發生在每次一個Session失效的時候。

這個接口也只包含兩個方法,分別對應於Session的建立和失效:
# public void sessionCreated(HttpSessionEvent se); 
# public void sessionDestroyed(HttpSessionEvent se);web

 

個人web應用上想知道到底有多少用戶在使用?瀏覽器

 

在網站中常常須要進行在線人數的統計。過去的通常作法是結合登陸和退出功能,即當用戶輸入用戶名密碼進行登陸的時候計數器加1,而後當用戶點擊退出按鈕退出系統的時候計數器減1。這種處理方式存在一些缺點,例如:用戶正常登陸後,可能會忘記點擊退出按鈕,而直接關閉瀏覽器,致使計數器減1的操做沒有及時執行;網站上還常常有一些內容是不須要登陸就能夠訪問的,在這種狀況下也沒法使用上面的方法進行在線人數統計。
  咱們能夠利用Servlet規範中定義的事件監聽器(Listener)來解決這個問題,實現更準確的在線人數統計功能。對每個正在訪問的用戶,J2EE應用服務器會爲其創建一個對應的HttpSession對象。當一個瀏覽器第一次訪問網站的時候,J2EE應用服務器會新建一個HttpSession對象 ,並觸發 HttpSession建立事件 ,若是註冊了HttpSessionListener事件監聽器,則會調用HttpSessionListener事件監聽器的sessionCreated方法。相反,當這個瀏覽器訪問結束超時的時候,J2EE應用服務器會銷燬相應的HttpSession對象,觸發 HttpSession銷燬事件,同時調用所註冊HttpSessionListener事件監聽器的sessionDestroyed方法。服務器

Java代碼   收藏代碼
  1. import javax.servlet.http.HttpSessionListener;  
  2. import javax.servlet.http.HttpSessionEvent;  
  3.   
  4. public class SessionCounter implements HttpSessionListener {  
  5. private static int activeSessions =0;  
  6. /* Session建立事件 */  
  7. public void sessionCreated(HttpSessionEvent se) {  
  8.       ServletContext ctx = event.getSession( ).getServletContext( );  
  9.         Integer numSessions = (Integer) ctx.getAttribute("numSessions");  
  10.         if (numSessions == null) {  
  11.             numSessions = new Integer(1);  
  12.         }  
  13.         else {  
  14.             int count = numSessions.intValue( );  
  15.             numSessions = new Integer(count + 1);  
  16.         }  
  17.         ctx.setAttribute("numSessions", numSessions);  
  18. }  
  19. /* Session失效事件 */  
  20. public void sessionDestroyed(HttpSessionEvent se) {  
  21.  ServletContext ctx=se.getSession().getServletContext();  
  22.  Integer numSessions = (Integer)ctx.getAttribute("numSessions");  
  23. <span class="oblog_text">        if(numSessions == null)  
  24.             numSessions = new Integer(0);  
  25.         }  
  26.         else {  
  27.             int count = numSessions.intValue( );  
  28.             numSessions = new Integer(count - 1);  
  29.         }  
  30.         ctx.setAttribute("numSessions", numSessions);</span>  
  31.   
  32.   
  33.   
  34. }  
  35. }  

  在這個解決方案中,任何一個Session被建立或者銷燬時,都會通知SessionCounter 這個類,固然通知的緣由是必須在web.xml文件中作相關的配置工做。以下面的配置代碼:session

Java代碼   收藏代碼
  1. <listener>  
  2.     <listener-class>demo.listener.SessionCounter</listener-class>  
  3. </listener>  

  如下兩種狀況下就會發生sessionDestoryed(會話銷燬)事件:
   1.執行session.invalidate()方法時 。
      既然LogoutServlet.java中執行session.invalidate()時,會觸發sessionDestory()從在線用戶 列表中清除當前用戶,咱們就沒必要在LogoutServlet.java中對在線列表進行操做了,因此LogoutServlet.java的內容如今是 這樣。jsp

Java代碼   收藏代碼
  1. public void doGet(HttpServletRequest request,HttpServletResponse response)  
  2.     throws ServletException, IOException {  
  3.     // 銷燬session  
  4.     request.getSession().invalidate();  
  5.     // 成功  
  6.     response.sendRedirect("index.jsp");  
  7. }  

 

   2.
      若是用戶長時間沒有訪問服務器,超過了會話最大超時時間 ,服務器就會自動銷燬超時的session。
      會話超時時間能夠在web.xml中進行設置,爲了容易看到超時效果,咱們將超時時間設置爲最小值。ide

Java代碼   收藏代碼
  1. <session-config>  
  2.     <session-timeout>1</session-timeout>  
  3. </session-config>  

 

      時間單位是一分鐘,而且只能是整數,若是是零或負數,那麼會話就永遠不會超時。網站

 

2.HttpSessionEventthis

這是類表明一個web應用程序內更改會話事件通知。spa

 

Java代碼   收藏代碼
  1. public class ShopSessionListener implements HttpSessionListener {  
  2.       
  3.     public void sessionCreated(HttpSessionEvent se) {  
  4.   
  5.     }     
  6.     public void sessionDestroyed(HttpSessionEvent se) {  
  7.         String sessionid = se.getSession().getId();  
  8.         EopSite site  =(EopSite)ThreadContextHolder.getSessionContext().getAttribute("site_key");  
  9.           
  10.         if(site!=null){  
  11.         ICartManager cartManager = SpringContextHolder.getBean("cartManager");  
  12.         cartManager.clean(sessionid,site.getUserid(),site.getId());  
  13.         }  
  14.     }  
  15. }  

 

se.getSession().getId();

HttpSession 接口中的getId():

          Returns a string containing the unique identifier assigned to this session.

          返回一個字符串,其中包含惟一標識符分配給本次會話。

相關文章
相關標籤/搜索