使用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("销毁一个会话");}}


不知道是什么问题????

原创粉丝点击