使用HttpSessionListener和ServletContextListener實現線上人數和歷史訪問人數統計的問題
阿新 • • 發佈:2018-12-31
以下是我的程式,但是重新開啟瀏覽器線上人數不會變化,歷史訪問人數只會在開啟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("銷燬一個會話"); } }
不知道是什麼問題????