HttpSessionListener :
Session建立事件發生在每次一個新的session建立的時候,相似地Session失效事件發生在每次一個Session失效的時候。
這個接口也只包含兩個方法,分別對應於Session的建立和失效:
# public void sessionCreated(HttpSessionEvent se);
# public void sessionDestroyed(HttpSessionEvent se);java
個人web應用上想知道到底有多少用戶在使用?web
在網站中常常須要進行在線人數的統計。過去的通常作法是結合登陸和退出功能,即當用戶輸入用戶名密碼進行登陸的時候計數器加1,而後當用戶點擊退出按鈕退出系統的時候計數器減1。這種處理方式存在一些缺點,例如:用戶正常登陸後,可能會忘記點擊退出按鈕,而直接關閉瀏覽器,致使計數器減1的操做沒有及時執行;網站上還常常有一些內容是不須要登陸就能夠訪問的,在這種狀況下也沒法使用上面的方法進行在線人數統計。
咱們能夠利用Servlet規範中定義的事件監聽器(Listener)來解決這個問題,實現更準確的在線人數統計功能。對每個正在訪問的用戶,J2EE應用服務器會爲其創建一個對應的HttpSession對象。當一個瀏覽器第一次訪問網站的時候,J2EE應用服務器會新建一個HttpSession對象,並觸發 HttpSession建立事件,若是註冊了HttpSessionListener事件監聽器,則會調用HttpSessionListener事件監聽器的sessionCreated方法。相反,當這個瀏覽器訪問結束超時的時候,J2EE應用服務器會銷燬相應的HttpSession對象,觸發 HttpSession銷燬事件,同時調用所註冊HttpSessionListener事件監聽器的sessionDestroyed方法。瀏覽器
import javax.servlet.http.HttpSessionListener; session
import javax.servlet.http.HttpSessionEvent; jsp
public class SessionCounter implements HttpSessionListener { ide
private static int activeSessions =0; 網站
/* Session建立事件 */ this
public void sessionCreated(HttpSessionEvent se) { spa
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>
}
}
在這個解決方案中,任何一個Session被建立或者銷燬時,都會通知SessionCounter 這個類,固然通知的緣由是必須在web.xml文件中作相關的配置工做。以下面的配置代碼:
<listener>
<listener-class>demo.listener.SessionCounter</listener-class>
</listener>
如下兩種狀況下就會發生sessionDestoryed(會話銷燬)事件:
1.執行session.invalidate()方法時。
既然LogoutServlet.java中執行session.invalidate()時,會觸發sessionDestory()從在線用戶 列表中清除當前用戶,咱們就沒必要在LogoutServlet.java中對在線列表進行操做了,因此LogoutServlet.java的內容如今是 這樣。
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
// 銷燬session
request.getSession().invalidate();
// 成功
response.sendRedirect("index.jsp");
}
2.
若是用戶長時間沒有訪問服務器,超過了會話最大超時時間,服務器就會自動銷燬超時的session。
會話超時時間能夠在web.xml中進行設置,爲了容易看到超時效果,咱們將超時時間設置爲最小值。
<session-config>
<session-timeout>1</session-timeout>
</session-config>
時間單位是一分鐘,而且只能是整數,若是是零或負數,那麼會話就永遠不會超時。
2.HttpSessionEvent
這是類表明一個web應用程序內更改會話事件通知。
public class ShopSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
}
public void sessionDestroyed(HttpSessionEvent se) {
String sessionid = se.getSession().getId();
EopSite site =(EopSite)ThreadContextHolder.getSessionContext().getAttribute("site_key");
if(site!=null){
ICartManager cartManager = SpringContextHolder.getBean("cartManager");
cartManager.clean(sessionid,site.getUserid(),site.getId());
}
}
}
se.getSession().getId();
HttpSession 接口中的getId():
Returns a string containing the unique identifier assigned to this session.
返回一個字符串,其中包含惟一標識符分配給本次會話。