JavaWeb Listener

1. What is a listener?

(1) The listener is a member of the Servlet specification. Just like Filter. Filter is also a member of the Servlet specification.
(2) In Servlet, all listener interfaces end with "Listener".
(3) The methods in the listener do not need to be called manually by the programmer. They are called by the server after a special event occurs

2. What is the use of a listener?

(1) The listener is actually a special opportunity left by the Servlet specification for us Javaweb programmers to execute a certain piece of code.
(2) If you want to execute a piece of code at a special moment, you need to think of using the corresponding listener.

3.What listeners are provided in the Servlet specification?

3.1 Under the jakarta.servlet package

(1)ServletContextListener

public interface ServletContextListener extends EventListener {
    
    
    public default void contextInitialized(ServletContextEvent sce) {
    
    
	// 这个方法是在ServletContext对象被创建的时候调用
    }
    public default void contextDestroyed(ServletContextEvent sce) {
    
    
    // 这个方法是在ServletContext对象被销毁的时候调用
    }
}

(2)ServletContextAttributeListener
(3)ServletRequestListener

public interface ServletRequestListener extends EventListener {
    
    
    public default void requestDestroyed (ServletRequestEvent sre) {
    
    
    // request对象销毁时间点被调用
    }
    public default void requestInitialized (ServletRequestEvent sre) {
    
    
    // request对象创建时间点被调用
} }

(4)ServletRequestAttributeListener

2.2 Under the jakarta.servlet.http package

(1)HttpSessionListener

public interface HttpSessionListener extends EventListener {
    
    
    public default void sessionCreated(HttpSessionEvent se) {
    
    
     // session创建的时候被调用
    }
    public default void sessionDestroyed(HttpSessionEvent se) {
    
    
   // session销毁的时候被调用
    }
}

(2) HttpSessionAttributeListener
This listener monitors changes in data in the session domain. As long as the data changes, the corresponding method is executed. The main monitoring point is on the session domain object.

public interface HttpSessionAttributeListener extends EventListener {
    
    
    public default void attributeAdded(HttpSessionBindingEvent se) {
    
    
    // 向session域中存储数据的时候,该方法被WEB服务器调用
    }
    public default void attributeRemoved(HttpSessionBindingEvent se) {
    
    
   // 将session域中存储的数据删除的时候,该方法被WEB服务器调用
    }
    public default void attributeReplaced(HttpSessionBindingEvent se) {
    
    
   // session域中某个数据被替换的时候,该方法被WEB服务器调用
    }
}

(3)HttpSessionBindingListener
● This listener does not need to be annotated with @WebListener.
● Assuming that the User class implements this listener, then the "binding" event is triggered when the User object is put into the session, and is triggered when the User object is deleted from the session
"Unbind" event.

@Data
public class User implements HttpSessionBindingListener {
    
    
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
    
    
        // 绑定数据时调用
    }
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
    
    
		// 解绑数据时调用
    }
    // 构造方法, Getter/Setter方法
    private String username;
    private Integer password;
}

(4) HttpSessionIdListener
● When the session id changes, the only method in the listener will be called.
(5) HttpSessionActivationListener
● Monitors the passivation and activation of session objects.
● Passivation: The session object is stored from the memory to the hard disk file.
● Activation: Restore the session from the hard disk file to the memory.

4. Steps to implement a listener

Take ServletContextListener as an example:
(1) Step 1: Write a class such as MyServletContextListener to implement the ServletContextListener interface. And implement the methods inside.

void contextInitialized(ServletContextEvent event)
void contextDestroyed(ServletContextEvent event)

(2) Step 2: Configure the ServletContextListener in the web.xml file, as follows:

<listener>
  <listener-class>com.bjpowernode.javaweb.listener.MyServletContextListener</listener-class>
</listener>

● Of course, you can also use annotations instead of configuration files in the second step, for example: @WebListener
● Note: All methods in the listener do not require javaweb What is called by the programmer is called by the server. When a special event occurs
(in fact, a certain time has arrived), it is automatically called by the web server.

Guess you like

Origin blog.csdn.net/y516369/article/details/132282108