Getting started with Java Web listener

monitor

Catalog

OnlineCountListener.java

package com.huangdekai.JavaWeb.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @Autord: HuangDekai
 * @Date: 2020/4/28 22:41
 * @Version: 1.0
 * @since: jdk11
 */
public class OnlineCountListener implements HttpSessionListener {

    // Create session listening
    // This event will be triggered once the Session is created
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
        Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");

        if (onlineCount == null) {
            onlineCount = 1;
        }
        else{
            onlineCount += 1;
        }

        servletContext.setAttribute("OnlineCount",onlineCount);
    }

    // Destroy session monitoring
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
        Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");

        if (onlineCount == null) {
            onlineCount = 0;
        }
        else {
            onlineCount -= 1;
        }

        servletContext.setAttribute("OnlineCount",onlineCount);
    }
}

The idea is to obtain a value with the key OnlineCount from the serveretcontext. Because the Session listener triggers the sessionCreated every time a Session is created, it will be acquired when there is a Session creation (indicating that there is an online Session). If it is empty, it will be proved that there is no Session, and the value of creating one is set to 1; if it is not empty, it will be proved that there is a Session, and the key is the value of OnlinCount + 1 . Finally, submit the updated value to ServletContext.

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>Current online population:<span><%=getServletConfig().getServletContext().getAttribute("OnlineCount")%></span></h2>
</body>
</html>

The front page displays the value.

web.xml

Similar to servlet s or filters, you configure the path in 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"
         metadata-complete="true">

<listener>
  <listener-class>com.huangdekai.JavaWeb.listener.OnlineCountListener</listener-class>
</listener>

</web-app>

Start Tomcat after configuration.

Result

As you can see, the number of online people is 2 (other cases are 3), and the problems involved here have not been solved. It is speculated that there are some actions in Tomcat to restart after closing the browser.

It can be eliminated by hot deployment - > refresh.

According to the characteristics of Session, the same browser accesses this page multiple times:

Access with another browser:

Refresh the original browser:

Keywords: Java Session xml JavaEE

Added by vin_akleh on Wed, 29 Apr 2020 18:08:47 +0300