session統計在線人數


用監聽器監聽訪問網站的事件,難以實現服務器端驗證游覽器端是否關閉游覽器,所以模擬session過期設置session的生命周期。

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;

public class ServletListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public ServletListener() {
    }
    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    //session創建時人數自增
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        HttpSession session = se.getSession();
        session.setMaxInactiveInterval(3*60);
        ServletContext servletContext = session.getServletContext();
        int count = (int) servletContext.getAttribute("count");
        servletContext.setAttribute("count",++count);
        System.out.println("調用了sessionCreate方法!!");
        System.out.println("當前在線人數:"+count);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        ServletContext servletContext = se.getSession().getServletContext();
        int count = (int) servletContext.getAttribute("count");
        servletContext.setAttribute("count",--count);
        System.out.println("調用了sessionDestroy方法!!");
        System.out.println("當前在線人數:"+count);
    }

    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("count",0);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

在web.xml中注冊監聽器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <listener>
        <listener-class>ServletListener</listener-class>
    </listener>
</web-app>

用不同的游覽器模擬不同的用戶訪問網站

 

百度網盤鏈接:鏈接: https://pan.baidu.com/s/10ifAib4iOwb9T1lO-fhl0Q 提取碼: ijfa 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM