使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题

以下是我的程序,但是重新打开浏览器在线人数不会变化,历史访问人数只会在开启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 block
			e1.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.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 block
			e1.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.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("销毁一个会话");

	}

}


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值