日常笔记——关于在线人数统计问题
2017-07-13 13:16
363 查看
SessionCounter.java
配置web.xml(要写在配置文件较前位置,先执行,此处写在SpringMvc的Listerner之前)
销毁session时间配置——一分钟
总结:sessionDestroyed()不执行的原因
1、listener在web.xml的配置文件中放置位置不正确
2、销毁时间过长,需设置销毁时间
package com.my.count; import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.springframework.context.ApplicationContext; /** * Application Lifecycle Listener implementation class SessionCounter * */ @WebListener public class SessionCounter implements HttpSessionListener { //total为访问总人数,onLineUser为在线人数 private static int onLineUser=0; public static int total=82553; public SessionCounter() { // TODO Auto-generated constructor stub } @Override public void sessionCreated(HttpSessionEvent event) { System.out.println("sessionCreated"); ServletContext sct = event.getSession().getServletContext(); Integer onLineUser = (Integer) sct.getAttribute("onLineUser"); if (onLineUser== null) { onLineUser = new Integer(1); }else { int count = onLineUser.intValue(); onLineUser = new Integer(count+1); } /*Integer total = (Integer) sct.getAttribute("total"); int countTotal = total.intValue(); total = new Integer(countTotal+1);*/ //Integer total = (Integer) sct.getAttribute("total"); sct.setAttribute("onLineUser", onLineUser); sct.setAttribute("total", total++); } @Override public void sessionDestroyed(HttpSessionEvent event){ System.out.println("sessionDestroyed"); ServletContext sct = event.getSession().getServletContext(); Integer onLineUser = (Integer) sct.getAttribute("onLineUser"); if (onLineUser == null) { onLineUser = new Integer(0); }else { int count = onLineUser.intValue(); onLineUser = new Integer(count-1); System.out.println("----------------------"+onLineUser); } // Integer total = (Integer) sct.getAttribute("total"); sct.setAttribute("onLineUser", onLineUser); sct.setAttribute("total", total); //销毁session // HttpSession session = event.getSession(); // session.invalidate(); } public static int getCount() { return onLineUser; } public static void setCount(int onLineUser) { SessionCounter.onLineUser =onLineUser; } }
配置web.xml(要写在配置文件较前位置,先执行,此处写在SpringMvc的Listerner之前)
` <listener> <listener-class> com.my.count.SessionCounter </listener-class> </listener> /////////////////////////// <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <session-config> <session-timeout>1</session-timeout> </session-config> //销毁session时间配置------一分钟
销毁session时间配置——一分钟
总结:sessionDestroyed()不执行的原因
1、listener在web.xml的配置文件中放置位置不正确
2、销毁时间过长,需设置销毁时间
相关文章推荐
- 关于在线人数的统计
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- 关于portal中的在线人数统计
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- 在线人数统计,解决了关闭浏览器窗口,释放session的问题
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- jsp统计在线人数问题
- 依据登录表统计累计在线人数(登录表去重问题)
- 使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
- Session应用问题-统计在线人数
- 发现一大把关于在线人数统计的文章
- 用fso和session统计在线人数 文档管理系统 V2.0
- 在线人数统计方法
- 利用HttpSessionListener实现网站在线人数统计功能
- 统计在线人数
- HttpSessionListener统计在线人数
- 在线人数统计 V1.0(Asp.net+ SqlServer) 源码下载
- 利用HttpSessionListener实现网站在线人数统计功能
- asp论坛在线人数统计研究
- 利用HttpSessionListener实现网站在线人数统计功能[摘]