使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
来源:互联网 发布:淘宝限时秒杀问题 编辑:程序博客网 时间:2024/04/30 03:51
以下是我的程序,但是重新打开浏览器在线人数不会变化,历史访问人数只会在开启Tomcat报错重启服务器后才会变化
package com.test.listener;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.http.HttpSessionContext;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import com.test.databasepool.CreatePool;import com.test.databasepool.DataBaseDao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class OnlineListener implements ServletContextListener,HttpSessionListener { Connection con;PreparedStatement ps;public void contextDestroyed(ServletContextEvent arg0) {System.out.println("销毁容器"); Long count = (Long) arg0.getServletContext().getAttribute("history");System.out.println("contextDestory count="+count);con = new CreatePool().getConnection();try {ps = con.prepareStatement("update history set historynum =" + count);ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}try {ps.close();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void contextInitialized(ServletContextEvent arg0) { System.out.println("初始化容器");arg0.getServletContext().setAttribute("online", 0);con = new CreatePool().getConnection();Long count = 0l; try {ps = con.prepareStatement("select historynum from history ");//ps.executeQuery()();java.sql.ResultSet rs = (ResultSet) ps.executeQuery();while (rs.next()){count = rs.getLong("historynum");System.out.println(count);}} catch (Exception e) {e.printStackTrace();}try {ps.close();con.close();} catch (Exception e) {e.printStackTrace();}arg0.getServletContext().setAttribute("history", count);}public void sessionCreated(HttpSessionEvent se) {System.out.println("初始化Session");//ServletContext application = Long c = (Long)se.getSession().getServletContext().getAttribute("history");System.out.println("history c="+c);se.getSession().getServletContext().setAttribute("history", c+1l);Long c2 = (Long)se.getSession().getServletContext().getAttribute("online");se.getSession().getServletContext().setAttribute("online", c2+1l);con = new CreatePool().getConnection();try {ps = con.prepareStatement("update history set historynum =" + se.getSession().getServletContext().getAttribute("history"));ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}try {ps.close();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void sessionDestroyed(HttpSessionEvent se) {ServletContext application = se.getSession().getServletContext();Long num=(Long)application.getAttribute("online");application.setAttribute("online",num-1l); se.getSession().invalidate(); System.out.println("销毁一个会话");}}
不知道是什么问题????
- 使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
- 使用ServletContextListener和HttpSessionListener两种监听器实现记录当前网站在线人数
- 在线人数和历史访问人数
- HttpSessionListener实现统计在线人数
- 使用HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- httpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- 使用HttpSessionListener接口监听Session的创建和失效(可以用于统计在线人数)
- struts2 实现在线人数和访问源的统计。
- 网站在线人数以及历史访问人数的统计代码
- 网站在线人数以及历史访问人数的统计代码
- Win7上安装IIS7并运行.net4.0程序
- iOS ShardSDK-如何自定义分享菜单中的平台列表
- EXPDE/IMPDP使用详解
- hdu 4119 Isabella's Message(模拟+字典树)
- linux常用命令之文件目录操作命令
- 使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
- eclipse导入jar包的方法
- EJB 3 Introduction
- 1065. A+B and C (64bit)
- linux常用命令之文件显示命令
- 1067. Sort with Swap(0,*)
- 数据库配置文件读取代码整理
- CUDA, 用于大量数据的超级运算:第九节
- SSH反向连接及Autossh