Listener listener

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Listener listener

There is something called a listener in JavaWeb. This listener is mainly used to monitor three objects: HttpServletRequest, HttpSession, and ServletContext. Each object corresponds to its own listener interface. The listener can monitor the events of the object creation, initialization and destruction information in these three objects, and can also monitor the events of the attribute change information in the object, such as the change information such as the attribute addition or deletion of the object.
So there will be six main listener interfaces, ServletRequestListener, ServletContextListener and HttpSessionListener interfaces are used to monitor the creation and destruction of objects.
ServletContextAttributeListener, HttpSessionAttributeListener and ServletRequestAttributeListener, these three interfaces define three methods to handle the addition, deletion and replacement of attributes in the monitored object. The same event has the same method name in these three interfaces. It's just that the accepted parameter types are different.
There are two ways to configure the listener. The first is to configure the listener in the form of annotations, and the second is to configure it in the form of the web.xml file. Refer to the following example:
Configuring listeners with annotations:

image

Configuring listeners using annotations is very simple, implement one or more listener interfaces, and then write the @WebListener annotation.

Configure the listener using the web.xml file:

image

Code:

image

The above code just implements the interface to monitor the Session object. This interface can monitor the creation and destruction of objects, and then write a Servlet class to test whether it can monitor normally. Code example:

image

operation result:

image

Since in the above example I used annotations and web.xml to configure two listeners, a session object is monitored by two listeners.

The following demonstrates the other two listener interfaces:
ServletRequestListener interface:

image

operation result:

image

ServletContextListener interface:

image

operation result:

image

image

As shown in the figure, since the ServletContext is created when the server is started and destroyed when the server is shut down, the creation and destruction of the ServletContext object can be monitored only in these two cases.

The above demonstrates the three interfaces for the creation and destruction of the listener object. Let's take a look at the three interfaces of the listener object attribute:
HttpSessionAttributeListener interface:

image

Servlet code:

image

operation result:

image

As shown in the figure, you can see that after the session object is destroyed, the attributes will be deleted, and this process will be monitored by the listener, so the listener can also help us view some rules and sequences of these objects.

ServletRequestAttributeListener接口:

image

Servlet code:

image

operation result:

image

ServletContextAttributeListener接口:

image

Servlet code:

image

operation result:

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484974&siteId=291194637