11 Listener of java web core

Keywords: JavaEE

2.11.1 listener introduction

First of all, we should understand the observer design pattern. All listeners are based on the observer design pattern.
Three components
Event source: the object that triggers the event
Event: the triggered action encapsulates the event source
Listener: when the event source triggers an event, the function can be completed

In the program, we can listen to the creation and destruction of objects, changes in attributes in domain objects, and session related content. There are 8 listeners in the Servlet specification. The listeners are provided in the form of interfaces, and the specific functions need to be completed by ourselves.

2.11.2 listener of listening object

ServletContextListener: used to listen for the creation and destruction of ServletContext objects.
In other words, as long as our application context object is created and destroyed, it can be listened to by this object.
Core method

Parameter: ServletContextEvent represents the event object
The event object encapsulates the event source, that is, ServletContext
Real events refer to the creation and destruction of ServletContext objects

HttpSessionListener: used to listen for the creation and destruction of HttpSession objects
Core method

Parameter: HttpSessionEvent represents the event object
The event object encapsulates the event source, that is, HttpSession
The real event refers to the operation of creating or destroying the HttpSession object

ServletRequestListener: used to listen for the creation and destruction of ServletRequest objects
Core method

Parameter: ServletRequestEvent represents the event object
The event object encapsulates the event source, that is, ServletRequest
A real event is the creation or destruction of a ServletRequest object

2.11.3 listener listening for domain object attribute changes

ServletContextAttributeListener: used to listen for attribute changes in the ServletContext application domain
Core method

Parameter: ServletContextAttributeEvent represents the event object. The event object encapsulates the event source, that is, ServletContext. The real event refers to the operations of adding, removing and replacing attributes in the application domain.

HttpSessionAttributeListener: used to listen for attribute changes in the HttpSession session domain
Core method

Parameter: HttpSessionBindingEvent represents the event object
The event object encapsulates the event source, that is, HttpSession
Real events refer to the operations of adding, removing and replacing attributes in the session domain.

ServletRequestAttributeListener: used to listen for attribute changes in the ServletRequest request domain
Core method
Parameter: ServletRequestAttributeEvent represents the event object
The event object encapsulates the event source, that is, ServletRequest
Real events refer to the operations of adding, removing and replacing attributes in the request domain.

2.11.4 listening to session related perceptual listeners

Perceptual listeners are defined listeners that can be used without configuration documents and annotations.
HttpSessionBindingListener: listener for sensing object and session domain binding
Core method

Parameter: HttpSessionBindingEvent represents the event object
The event object encapsulates the event source, that is, HttpSession
Real events refer to the operations of adding and removing data in the session domain.

HttpSessionActivationListener: a listener used to sense the passivation and activation of objects in the session domain.
Passivation refers to serialization, persistence, and activation is the normal state.

Parameter: HttpSessionEvent represents the event object
The event object encapsulates the event source, that is, HttpSession
Real events refer to data passivation and activation operations in the session domain.

Use of listener

Of the listening object
ServletContextListener
HttpSessionListener
ServletRequestListener

package com.symc.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/11/17 20:07
 * @Description: ServletContext Listener for object creation and destruction
 * be careful:
 * You need to implement the ServletContextListener interface in the javax.servlet package
 * The method to override is not required because it is the default method
 * Configuration required:
 *  Annotation @ WebListener
 *  ServletContext As our application loads, it is created
 */
@WebListener
public class ServletContextListenerDemo implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Monitor ServletContext Object creation");
        //Get object
        ServletContext servletContext = sce.getServletContext();
        System.out.println(servletContext);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Monitor ServletContext Destruction of objects");
    }
}

Configure tomcat, start the server, and observe the console

When we shut down the server, the object is destroyed

Listening for attribute changes
ServletContextAttributeListener
HttpSessionAttributeListener
ServletRequestAttributeListener

package com.symc.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/11/17 20:24
 * @Description: Listener for attribute changes in application domain objects
 */
@WebListener
public class ServletContextAttributeListenerDemo implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("Listening for attribute addition");
        //get attribute
        ServletContext servletContext = scae.getServletContext();
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("Property substitution detected");
        ServletContext servletContext = scae.getServletContext();
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("Property removal was monitored");
        ServletContext servletContext = scae.getServletContext();
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }
}

We also need to add a Servlet to add, modify and delete properties

package com.symc.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/11/17 20:33
 * @Description:
 */
@WebServlet("/servletContext")
public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        servletContext.setAttribute("username","Gosling");
        servletContext.setAttribute("username","Fengwen");
        servletContext.removeAttribute("username");
    }
}

Start the server, access the Servlet through the browser, and look at the console.

Previously, it was in the form of annotation. Now let's talk about the way to use the configuration document:

    <listener>
        <listener-class>com.symc.listener.ServletContextAttributeListenerDemo</listener-class>
    </listener>
    
    <listener>
        <listener-class>com.symc.listener.ServletContextListenerDemo</listener-class>
    </listener>

It's easy to understand, so I won't introduce it anymore.

Session related perceptual
HttpSessionBindingListener
HttpSessionActivationListener
You need to know that the perceptual does not need configuration documents or annotations.

Posted by nuttynibbles on Wed, 17 Nov 2021 05:13:33 -0800