Those who have written AWT or swing programs in JSP/servlet must be impressed with the event handling mechanism of the desktop program: classes that implement the listener interface can be used when a specific event occurs, call specific methods to respond to events.
In fact, we also have a similar event processing mechanism when writing JSP/servle programs. The difference is that in JSP/servlet, it is in the web. listener is registered in XML, and the container calls a specific class to implement listener when a specific event occurs.
1. listener and event in servlet:
In JSP 2.0/servlet 2.4, there are eight listener interfaces and six event categories.
Servletcontextlistener Interface
[Interface method] contextinitialized () and contextdestroyed ()
[Receive events] servletcontextevent
[Trigger scenario] contextinitialized () is called when the container loads a web application (for example, after the container is started), and The contextdestroyed () method is called when the container removes the web application.
Servletcontextattributelistener
[Interface method] attributeadded (), attributereplaced (), attributeremoved ()
[Receive events] servletcontextattributeevent
[Trigger scenario] if an object is added as the attribute of the application (servletcontext) object, attributeadded () is called. Similarly, attributereplaced () is called when attributes are replaced or removed () attributeremoved ().
Httpsessionlistener
[Interface method] sessioncreated () and sessiondestroyed ()
[Receive events] httpsessionevent
[Trigger scenario] When a session (httpsession) object is created or eliminated, the two methods are called respectively.
Httpsessionattributelistener
[Interface method] attributeadded (), attributereplaced (), attributeremoved ()
[Receive events] httpsessionbindingevent
[Trigger scenario] if an object is added as the attribute of the session (httpsession) object, attributeadded () is called. Similarly, attributereplaced () is called when the attribute is replaced or removed () attributeremoved ().
Httpsessionactivationlistener
[Interface method] sessiondidactivate () and sessionwillpassivate ()
[Receive events] httpsessionevent
[Trigger scenario] Activate and passivate are actions used to replace objects, when the session object must be temporarily stored to the hard disk or other storage devices for reasons such as resource utilization or load balancing (through object serialization), the action is called passivate, the Session object on the hard disk or storage device is called activate when the JVM is reloaded. It is easy to understand that sessiondidactivate () and sessionwillpassivate () call after activeate and before passivate respectively.
Servletrequestlistener
[Interface method] requestinitialized () and requestdestroyed ()
[Receive events] requestevent
[Trigger scenario] the two methods are called when the request (httpservletrequest) object is created or eliminated.
Servletrequestattributelistener
[Interface method] attributeadded (), attributereplaced (), attributeremoved ()
[Receive events] httpsessionbindingevent
[Trigger scenario] if an object is added as an attribute of the request (httpservletrequest) object, attributeadded () is called. Similarly, attributereplaced () is called when attributes are replaced or removed () attributeremoved ().
Httpsessionbindinglistener
[Interface method] valuebound () and valueunbound ()
[Receive events] httpsessionbindingevent
[Trigger scenario] class for implementing the httpsessionbindinglistener interface. For example, if it is added to the attribute of the session (httpsession) object, valuebound () is called. If it is received from the session (httpsession) when you remove an object from its attributes, the system calls valueunbound () to implement the httpsessionbindinglistener interface. set in XML.
2. How to register events in Servlet
In addition to httpsessionbindinglistener, you must register with the container in Web. xml before the container can call the corresponding category when the corresponding event occurs, for example:
<Listener>
<Listener-class> demo. servlet. listener. customservletcontextlistener </listener-class>
</Listener>
3. Java instances
============
// Listen for startup and Shutdown
Import javax. servlet. servletcontextlistener;
Import javax. servlet .*;
Public class tigerlisten implements servletcontextlistener {
Public void contextinitialized (servletcontextevent SCE)
{
System. Out. Print ("init ");
}
Public void contextdestroyed (servletcontextevent SCE)
{
System. Out. Print ("destroved ");
}
}
The corresponding web. XML is
================
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Listener>
<Listener-class> tigerlisten </listener-class>
</Listener>
</Web-app>