Servlet API provides basic application event listener interfaces. Programmers can use these interfaces to manage events. Servlet2.4 and later provide event listening for the following objects: servletcontext, httpsession, and servletrequest. Listening to servletcontext allows the web program to know the running status of the entire application, such as loading and uninstalling. Listening to httpsession allows the web program to understand the situation during the application and respond. Listening to servletrequest allows web programs to control the lifecycle of Web requests.
The following describes how to connect event listeners in conjunction with the official documents of assumervetapi.
1. servletcontextlistener
Package: javax. servlet
Interface declaration: public interface servletcontextlistener extends java. util. eventlistener
Method:
1. Void contextdestroyed (servletcontextevent SCE)
When the application is uninstalled, the code in this method is triggered.
2. Void contextinitialized (servletcontextevent SCE)
The code in this method is triggered when the application is initialized.
Here we need to describe servletcontentevent, which is an event of the servletcontext object. Its Class declaration is as follows:
Public class servletcontentevent extends java. util. eventobject
Includes: servletcontext getservletcontext (). It returns the servlet context of the event, that is, the current application that generates the event.
Ii. servletcontextattributelistener
Package: javax. servlet
Interface declaration: public interface servletcontextattributelistener
Extends java. util. eventlistener
Method:
1. Void attributeadded (servletcontextattributeevent scab)
When a new attribute is added to the servlet context, the code in this method is triggered.
2. Void attributeremoved (servletcontextattributeevent scab)
Code in this method is triggered when an attribute is removed from the servlet context.
3. Void attributereplaced (servletcontextattributeevent scab)
When the value of an attribute in the servlet context is replaced, the code in this method is triggered.
Servletcontextattributeevent is an attribute event in the servlet context. Its Class declaration is as follows:
Public class servletcontextattributeevent
Extends servletcontextevent
Include method: String getname (), return the property name of the event to be generated; object getvalue (), return the property value of the event to be generated.
Iii. httpsessionlistener
Package: javax. servlet
Interface declaration: public interface httpsessionlistener
Extends java. util. eventlistener
Method:
1. Void sessioncreated (httpsessionevent SE)
When a session is created, the code in this method is triggered.
2. Void sessiondestroyed (httpsessionevent SE)
When a session is released, the code in this method is triggered.
Httpsessionevent is a session event class and its declaration is as follows:
Public class httpsessionevent
Extends java. util. eventobject
Includes Methods: httpsession getsession (), which returns the session object that generates the event.
4. httpsessionactivationlistener
Package: javax. servlet
Interface declaration: public interface httpsessionactivationlistener
Extends java. util. eventlistener
Method:
1. Void sessiondidactivate (httpsessionevent SE)
2. Void sessionwillpassivate (httpsessionevent SE)
Activate and passivate are actions used to replace objects. When session objects must be temporarily stored on hard disks or other storage devices for reasons such as resource utilization or load balancing (serialized through objects ), the action is called passivate, and the action taken when the session object on the hard disk or storage device reloads the JVM is called activate.
V. httpsessionattributelistener
Package: javax. servlet
Interface declaration: public interface httpsessionattributelistener
Extends java. util. eventlistener
Method:
1. Void attributeadded (httpsessionbindingevent SE)
2. Void attributereplaced (httpsessionbindingevent SE)
3. Void attributeremoved (httpsessionbindingevent SE)
The preceding three methods are triggered when the session property is added, the session property value is modified, and the session property is removed.
Httpsessionbindingevent is a session event object class. Its declaration is as follows:
Public interface httpsessionbindinglistener
Extends java. util. eventlistener
Method: String getname (). The property name of the session that generates the current event is returned. Object getvalue (), returns the property value of the session that generates the current event. Httpsession getsession () returns the session object that generates the current event.
6. httpsessionbindinglistener
Package: javax. servlet
Interface declaration: public interface httpsessionbindinglistener
Extends java. util. eventlistener
Method:
1. Void valuebound (httpsessionbindingevent event)
This method is executed when the object implementing the httpsessionbindinglistener interface is bound to the session attribute.
2. Void valueunbound (httpsessionbindingevent event)
When the object implementing the httpsessionbindinglistener interface is unbound from the session attribute, this method is executed for this object.
Note the differences between httpsessionattributelistener and httpsessionbindinglistener:
1. The former needs to be described in Web. XML, and the latter does not.
2. the former triggers execution of the Code in its method when any session owner changes, the latter will trigger the code of the valuebound and valueunboundy methods of the object only when its object is bound to a session attribute or unbound from the session owner. For example, two objects A and B both implement the httpsessionbindinglistener interface. When a is bound to the session attribute, only the valuebound () method of A is triggered for execution.
7. servletrequestlistener
Package: javax. servlet
Interface declaration: public interface servletrequestlistener
Extends java. util. eventlistener
Method:
1. Void requestdestroyed (servletrequestevent EVT)
2. Void requestinitialized (servletrequestevent EVT)
The preceding two methods are triggered when the servetrequest object is initialized and cleared.
Servletrequestevent indicates the servletreuest event class. Its declaration is as follows:
Public class servletrequestevent
Extends java. util. eventobject
Includes Methods: servletcontext getservletcontext () to obtain the context object of the current web application. Servletrequest getservletrequest () obtains the subject of the current event, the servletrequest object.
VIII. servletrequestattributelistener
Package: javax. servlet
Interface declaration: public interface servletrequestattributelistener
Extends java. util. eventlistener
Method:
1. Void attributeadded (servletrequestattributeevent E)
This method is triggered when attributes are added to the attributes of the servlvetrequest object.
2. Void attributeremoved (servletrequestattributeevent E)
This method is triggered when the attribute is removed from the servlvetrequest object attribute.
3. Void attributereplaced (servletrequestattributeevent E)
This method is triggered when the owner value of the servlvetrequest object attribute is modified.
Servletrequestattributeevent is the event class of the servletrequest attribute. Its declaration is as follows:
Public class servletrequestattributeevent
Extends servletrequestevent
Include method: String getname () to obtain the name of the property of the trigger event. Object getvalue () to obtain the generated value of the trigger event.
The following describes how to deploy event listeners in Web. XML to process events. The format is as follows:
<Listener>
<Listener-class>
Fey. servlet. listener. customservletcontextlistener
</Listener-class>
</Listener>
Fey. servlet. listener. customservletcontextlistener is the class name that implements the above-mentioned event listener interfaces. Of course, you need to put these classes under the web application classes or lib directory of the Web container so that the Web Container can find them.
In addition, a class can have one or more listener interfaces.