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;
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");
if(numSessions == null)
numSessions = new Integer(0);
}
else {
int count = numSessions.intValue( );
numSessions = new Integer(count - 1);
}
ctx.setAttribute("numSessions", numSessions);
}
}
在这个解决方案中,任何一个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的内容如今是 这样。 session
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { // 销毁session request.getSession().invalidate(); // 成功 response.sendRedirect("index.jsp"); }
2. 若是用户长时间没有访问服务器,超过了会话最大超时时间 ,服务器就会自动销毁超时的session。 会话超时时间能够在web.xml中进行设置,为了容易看到超时效果,咱们将超时时间设置为最小值。 jsp
<session-config> <session-timeout>1</session-timeout> </session-config>
时间单位是一分钟,而且只能是整数,若是是零或负数,那么会话就永远不会超时。 ide
2.HttpSessionEvent 网站
这是类表明一个web应用程序内更改会话事件通知。 this
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(); spa
HttpSession 接口中的getId():
Returns a string containing the unique identifier assigned to this session.
返回一个字符串,其中包含惟一标识符分配给本次会话。