---
title: "使用HttpSessionListener接口监听Session的创建和失效"
author: "TrueBrian"
date: "2014-09-23T16:06:00Z"
tags: []
slug: "httpsessionlistener-session"
draft: false
lastmod: "2015-03-11T09:47:21Z"
id: 2191271
url: "https://truebrian.iteye.com/blog/2191271"
---

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代码[![收藏代码](http://uule.iteye.com/images/icon_star.png)]()

01. importjavax.servlet.http.HttpSessionListener;
02. importjavax.servlet.http.HttpSessionEvent;
03. publicclassSessionCounterimplementsHttpSessionListener{
04. privatestaticintactiveSessions=0;
05. /\*Session创建事件\*/
06. publicvoidsessionCreated(HttpSessionEventse){
07. ServletContextctx=event.getSession().getServletContext();
08. IntegernumSessions=(Integer)ctx.getAttribute("numSessions");
09. if(numSessions==null){
10. numSessions=newInteger(1);
11. }
12. else{
13. intcount=numSessions.intValue();
14. numSessions=newInteger(count+1);
15. }
16. ctx.setAttribute("numSessions",numSessions);
17. }
18. /\*Session失效事件\*/
19. publicvoidsessionDestroyed(HttpSessionEventse){
20. ServletContextctx=se.getSession().getServletContext();
21. IntegernumSessions=(Integer)ctx.getAttribute("numSessions");
22. &lt;spanclass="oblog\_text"&gt;if(numSessions==null)
23. numSessions=newInteger(0);
24. }
25. else{
26. intcount=numSessions.intValue();
27. numSessions=newInteger(count-1);
28. }
29. ctx.setAttribute("numSessions",numSessions);&lt;/span&gt;
30. }
31. }

在这个解决方案中，任何一个Session被创建或者销毁时，都会通知SessionCounter 这个类，当然通知的原因是必须在web.xml文件中做相关的配置工作。如下面的配置代码：

Java代码[![收藏代码](http://uule.iteye.com/images/icon_star.png)]()

1. &lt;listener&gt;
2. &lt;listener-class&gt;demo.listener.SessionCounter&lt;/listener-class&gt;
3. &lt;/listener&gt;

以下两种情况下就会发生sessionDestoryed（会话销毁）事件：  
1.执行session.invalidate()方法时。  
既然LogoutServlet.java中执行session.invalidate()时，会触发sessionDestory()从在线用户 列表中清除当前用户，我们就不必在LogoutServlet.java中对在线列表进行操作了，所以LogoutServlet.java的内容现在是 这样。

Java代码[![收藏代码](http://uule.iteye.com/images/icon_star.png)]()

1. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
2. throwsServletException,IOException{
3. //销毁session
4. request.getSession().invalidate();
5. //成功
6. response.sendRedirect("index.jsp");
7. }

2\.  
如果用户长时间没有访问服务器，超过了会话最大超时时间，服务器就会自动销毁超时的session。  
会话超时时间可以在web.xml中进行设置，为了容易看到超时效果，我们将超时时间设置为最小值。

Java代码[![收藏代码](http://uule.iteye.com/images/icon_star.png)]()

1. &lt;session-config&gt;
2. &lt;session-timeout&gt;1&lt;/session-timeout&gt;
3. &lt;/session-config&gt;

时间单位是一分钟，并且只能是整数，如果是零或负数，那么会话就永远不会超时。

2.HttpSessionEvent

这是类代表一个web应用程序内更改会话事件通知。

Java代码[![收藏代码](http://uule.iteye.com/images/icon_star.png)]()

01. publicclassShopSessionListenerimplementsHttpSessionListener{
02. publicvoidsessionCreated(HttpSessionEventse){
03. }
04. publicvoidsessionDestroyed(HttpSessionEventse){
05. Stringsessionid=se.getSession().getId();
06. EopSitesite=(EopSite)ThreadContextHolder.getSessionContext().getAttribute("site\_key");
07. if(site!=null){
08. ICartManagercartManager=SpringContextHolder.getBean("cartManager");
09. cartManager.clean(sessionid,site.getUserid(),site.getId());
10. }
11. }
12. }

se.getSession().getId();

HttpSession 接口中的getId():

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

返回一个字符串，其中包含唯一标识符分配给本次会话。