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


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

HttpSessionListener :

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

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

 

我的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文件中做相關的配置工作。如下面的配置代碼:

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的內容現在是 這樣。

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中進行設置,為了容易看到超時效果,我們將超時時間設置為最小值。

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

 

      時間單位是一分鍾,並且只能是整數,如果是零或負數,那么會話就永遠不會超時。

 

2.HttpSessionEvent

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

 

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.

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM