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


在網站中經常需要進行在線人數的統計。過去的一般做法是結合登錄和退出功能,即當用戶輸入用戶名密碼進行登錄的時候計數器加1,然后當用戶點擊退出按鈕退出系統的時候計數器減1。這種處理方式存在一些缺點,例如:用戶正常登錄后,可能會忘記點擊退出按鈕,而直接關閉瀏覽器,導致計數器減1的操作沒有及時執行;網站上還經常有一些內容是不需要登錄就可以訪問的,在這種情況下也無法使用上面的方法進行在線人數統計。

import javax.servlet.http.HttpSessionListener;  
import javax.servlet.http.HttpSessionEvent;  
  
public class SessionCounter implements HttpSessionListener {  
private static int activeSessions =0;  
/* Session創建事件 */  
public void sessionCreated(HttpSessionEvent se) {  
      ServletContext ctx = event.getSession( ).getServletContext( );  
        Integer numSessions = (Integer) ctx.getAttribute("numSessions");  
        if (numSessions == null) {  
            numSessions = new Integer(1);  
        }  
        else {  
            int count = numSessions.intValue( );  
            numSessions = new Integer(count + 1);  
        }  
        ctx.setAttribute("numSessions", numSessions);  
}  
/* Session失效事件 */  
public void sessionDestroyed(HttpSessionEvent se) {  
 ServletContext ctx=se.getSession().getServletContext();  
 Integer numSessions = (Integer)ctx.getAttribute("numSessions");  
<span class="oblog_text">        if(numSessions == null)  
            numSessions = new Integer(0);  
        }  
        else {  
            int count = numSessions.intValue( );  
            numSessions = new Integer(count - 1);  
        }  
        ctx.setAttribute("numSessions", numSessions);</span>  
  
  
  
}  
}  
import javax.servlet.http.HttpSessionListener;  
import javax.servlet.http.HttpSessionEvent;  
  
public class SessionCounter implements HttpSessionListener {  
private static int activeSessions =0;  
/* Session創建事件 */  
public void sessionCreated(HttpSessionEvent se) {  
      ServletContext ctx = event.getSession( ).getServletContext( );  
        Integer numSessions = (Integer) ctx.getAttribute("numSessions");  
        if (numSessions == null) {  
            numSessions = new Integer(1);  
        }  
        else {  
            int count = numSessions.intValue( );  
            numSessions = new Integer(count + 1);  
        }  
        ctx.setAttribute("numSessions", numSessions);  
}  
/* Session失效事件 */  
public void sessionDestroyed(HttpSessionEvent se) {  
 ServletContext ctx=se.getSession().getServletContext();  
 Integer numSessions = (Integer)ctx.getAttribute("numSessions");  
<span class="oblog_text">        if(numSessions == null)  
            numSessions = new Integer(0);  
        }  
        else {  
            int count = numSessions.intValue( );  
            numSessions = new Integer(count - 1);  
        }  
        ctx.setAttribute("numSessions", numSessions);</span>  
  
  
  
}  
}  
<listener>  
    <listener-class>test.listener.SessionCounter</listener-class>  
</listener> 

<session-config>  
    <session-timeout>1</session-timeout>  
</session-config>

參考地址:http://uule.iteye.com/blog/824115


免責聲明!

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



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