Servlet event Listener, listener ServletContext domain object creation and destruction, listening for three domain object property changes

Source: Internet
Author: User
Tags event listener

Servlet event Listener, listener ServletContext domain object creation and destruction, listening for three domain object property changes

Listener

A listener is an ordinary Java program that implements a particular interface, which is specifically designed to listen to a method call or property change of another Java object, and a listener method is executed immediately after the above event occurs on the Listener object.

Servle Listener

Several types of listeners are defined in the servlet specification, which are used to listen for three domain objects, ServletContext, HttpSession, and ServletRequest, respectively.

The servlet specification also divides these types of listeners into three types for operations on these three objects.

Monitor event listeners for three domain object creation and destruction

Listener for adding and deleting properties in a domain object

Listens for event listeners that are bound to the state of an object in the HttpSession domain. (View API documentation)

Monitoring ServletContext domain object creation and destruction

The Servletcontextlistener interface is used to listen for ServletContext object creation and destruction events.

When the ServletContext object is created, the contextinitialized (Servletcontextevent SCE) method is fired

The contextdestroyed (Servletcontextevent SCE) method is fired when the ServletContext object is destroyed.

When ServletContext domain objects are created and destroyed:

Create: Server start create servletcontext for each web App

Destroy: Close the ServletContext on behalf of each Web application before the server shuts down

Writing a Servlet listener

As with other event listeners, writing a servlet listener also requires implementing a specific interface and overwriting the corresponding method in the interface for the corresponding action.

Slightly different from other event listeners, the registration of the servlet listener is not registered directly on the event source, but is registered by the Web container, and the developer simply configures the listener with the <listener> tag in the. xml file. The Web container automatically registers the listener with the event source.

Multiple Servlet event listeners can be configured in a Web. xml file, and the server loads and registers these Serlvet event listeners in the order in which they are registered in the. xml file.

Monitoring HttpSession domain object creation and destruction

Httpsessionlistener interface for monitoring httpsession creation and destruction

When a session is created, the sessioncreated (httpsessionevent se) method is called.

When a session is destroyed, the sessiondestroyed (httpsessionevent se) method is called.

Session domain object creation and destruction time creation: The server creates a session each time the user accesses

Destroy: If the user's session is not used for 30 minutes, the server will destroy the session, we can also configure the session expiration time in Web. xml

The Servletrequestlistener interface is used to monitor the creation and destruction of ServletRequest objects.

When the Request object is created, the listener's Requestinitialized method is called.

When the request object is destroyed, the listener's Requestdestroyed method is called.

(Review the Request object here, refresh the access servlet more than once in the browser window, see the Request object's creation and destruction, write a servlet, and then jump to the other servlet in Sendredirect and forward ways, Viewing the creation and consumption of the request object)

ServletRequest domain object creation and destruction time:

Create: Each time a user accesses, a reqeust will be created

Destroy: The request object will be destroyed at the end of the current visit

Listening for three domain object property changes

The servlet specification defines the listener that listens to ServletContext, HttpSession, httpservletrequest attribute change information events in these three objects.

These three listener interfaces are Servletcontextattributelistener, Httpsessionattributelistener Servletrequestattributelistener

Each of the three interfaces defines three methods to handle the addition, deletion, and substitution of the attributes in the Listener object, the same event in the three interfaces corresponding to the same method name, except that the accepted parameter types are different.

Attributeadded method

When an attribute is added to the Listener object, the Web container invokes the Attributeadded method of the event listener, which accepts an event type parameter that the listener can use to obtain the domain object that is increasing the property and the Property object that is saved to the domain

The complete syntax in each domain property listener is defined as:

public void attributeadded (Servletcontextattributeevent scae)

public void attributeadded (Httpsessionbindingevent hsbe)

public void attributeadded (Servletrequestattributeevent srae)

Attributeremoved method

When a property is deleted from a listener, the Web container invokes this method of the event listener accordingly

The complete syntax in each domain property listener is defined as:

public void attributeremoved (Servletcontextattributeevent scae)

public void attributeremoved (Httpsessionbindingevent hsbe)

public void attributeremoved (Servletrequestattributeevent srae)

Attributereplaced method

When a property in the listener's domain object is replaced, the Web container invokes this method of the event listener accordingly

The complete syntax in each domain property listener is defined as:

public void attributereplaced (Servletcontextattributeevent scae)

public void attributereplaced (Httpsessionbindingevent hsbe)

public void attributereplaced (Servletrequestattributeevent srae)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.