How to use Servlet listener in personal web project?

Compilation software: IntelliJ IDEA 2019.2.4 x64
Operating system: win10 x64-bit Home Edition
Server software: apache-tomcat-8.5.27



1. What is a Servlet listener?

In official terms, "It is an object based on the Java Servlet specification, and is mainly used to monitor the creation and destruction events of domain objects such as ServletContext, HttpSession and HttpServletRequest in web applications , and to monitor the occurrence of properties in these domain objects. Modified event", a very incisive expression.

But these words may not be very understandable to novice Xiaobai. Don't worry, I personally understand that it is like a bug in our real life . It eavesdrops on certain groups and responds accordingly based on specific behavioral events of the target , such as the notorious US prism door hacking incident.


2. What are the functions of the Servlet listener?

2.1 Creation and destruction of listening domain objects

2.1.1 ServletContextListener interface

effect:

Monitor the creation and destruction of ServletContext objects (global context objects)

method name effect
contextInitialized(ServletContextEvent sce) Called when ServletContext is created
contextDestroyed(ServletContextEvent sce) Called when the ServletContext is destroyed

Note:

ServletContext对象It is created when the web project is loaded (the Tomcat server is turned on) , but it is destroyed when the web project is unloaded (the Tomcat server is turned off)

2.1.2 HttpSessionListener interface

effect:

Monitor the creation and destruction of HttpSession objects (session domain objects)

method name effect
sessionCreated(HttpSessionEvent hse) Called when the HttpSession object is created
sessionDestroyed(HttpSessionEvent hse) Called when the HttpSession object is destroyed

Note:

HttpSession对象It is created when the getSession method is called for the first time , but the object is destroyed when the client is closed, forcibly invalidated, and automatically invalidated when the maximum idle time is reached.

2.1.3 ServletRequestListener interface

effect:

Monitor the creation and destruction of ServletRequest objects (request domain objects)

method name effect
requestInitialized(ServletRequestEvent sre) Called when the ServletRequest object is created
requestDestroyed(ServletRequestEvent sre) Called when the ServletRequest object is destroyed

Note:

ServletRequest对象It is created when there is a request , but destroyed when the response ends.

2.2 Adding, modifying, and deleting shared data in monitoring domain objects

2.2.1 ServletContextAttributeListener interface

effect:

Monitor the addition, removal and modification of attributes in the ServletContext object

method name effect
attributeAdded(ServletContextAttributeEvent scab) Called when an attribute is added to the ServletContext
attributeRemoved(ServletContextAttributeEvent scab) Called when an attribute is removed from the ServletContext
attributeReplaced(ServletContextAttributeEvent scab) Called when an attribute in the ServletContext is modified

The ServletContextAttributeEvent object represents an attribute change event, and it contains the following methods:

method name effect
getName() Get the modified or added attribute name
getValue() Get the attribute value that was modified or added
getServletContext() Get the ServletContext object

2.2.2 HttpSessionAttributeListener interface

effect:

Monitor the addition, removal and modification of attributes in the HttpSession object

method name effect
attributeAdded(HttpSessionBindingEvent se) Called when adding attributes to HttpSession
attributeRemoved(HttpSessionBindingEvent se) Called when an attribute is removed from the HttpSession
attributeReplaced(HttpSessionBindingEvent se) Called when an attribute in the HttpSession is modified

The HttpSessionBindingEvent object represents an attribute change event, and it contains the following methods:

method name effect
getName() Get the modified or added attribute name
getValue() Get the attribute value that was modified or added
getSession() Get the HttpSession object that triggered the event

2.2.3 ServletRequestAttributeListener interface

effect:

Monitor the addition, removal and modification of attributes in the ServletRequest object

method name effect
attributeAdded(ServletRequestAttributeEvent srae) Called when an attribute is added to the ServletRequest
attributeRemoved(ServletRequestAttributeEvent srae) Called when an attribute is removed from a ServletRequest
attributeReplaced(ServletRequestAttributeEvent srae) Called when an attribute in the ServletRequest is modified

The ServletRequestAttributeEvent object represents an attribute change event, and it contains the following methods:

method name effect
getName() Get the modified or added attribute name
getValue() Get the attribute value that was modified or added
getServletRequest () Get the ServletRequest object that triggered the event

3. How to create a Servlet listener?

step:

①Create a class

② Implement an interface

According to the needs of business scenarios, you can choose to implement one of the three (ServletContextListener, HttpSessionListener, or ServletRequestListener) interfaces to use

③ Implement the abstract method in the interface

④ Register the listener (the most primitive approach)

Location:

Register in web.xml

The code example is as follows:

//注册刚创建的监听器MyListener
<listener>
    <listener-class>Listenner.MyListener</listener-class>
</listener>

4. Hands-on case

Case: Create a listener MyListener class, inherit the ServletContextListener interface, to monitor the creation and destruction of the global context object, and register the listener in web.xml. The presentation listener observes the creation and destruction of the global context object.

①Create the MyListener class and inherit the ServletContextListener interface

The code example is as follows:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

//监听ServletContext应用域对象
public class MyListener implements ServletContextListener {
    
    
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    
    
        System.out.println("ServletContext对象创建后执行");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    
    
        System.out.println("ServletContext对象销毁后执行");
    }
}

② Register the listener in web.xml

The code example is as follows:

<listener>
    <listener-class>Listener.MyListener</listener-class>
</listener>

insert image description here

Guess you like

Origin blog.csdn.net/siaok/article/details/130352741