About the listener listener in Java EE

Source: Internet
Author: User
Tags event listener java web

Listener is designed based on the observer pattern, listener provides a means of controlling control and data for servlet applications. Registration of Listener

Java Web Listener Registration is simpler because the objects and events that they listen to are created by the servlet container, and if their listeners are needed, they need to be configured in Web.xml:

[Java] view plain copy <!--web.xml--> <listener> <listener-class> ..... Implements custom listeners for specific interfaces 1</listener-class> <listener-class> .... Implement custom listener 2</listener-class> for specific interfaces ... </listener>


for multiple listener that implement the same interface, the execution is determined in the order in which they are registered in Web.xml .


Listener can also be added dynamically in the application.


Listener Related Interface

Several types of listener are defined in the servlet specification, and they are used to listen to the three domain objects of servletcontext,httpsession and ServletRequest, respectively .

The servlet specification divides this listener into three types for operations on these three objects:

event listeners listening for the creation and destruction of three domain objects

Listener class

The interface that contains

Interface description

Servletcontextlistener

Contextinitialized (Servletcontextevent SCE);


Contextdestroyed (Servletcontextevent SCE);

triggered when the context container is initialized, the Contextinitialized interface is first invoked before all of the filter and servlet init method calls;


The context container is destroyed, and the Contextdestroyed interface is invoked after all the filter and servlet destroy method calls;

Httpsessionlistener

sessioncreated (httpsessionevent se);
sessiondestroyed (htppsessionevent se);

triggered when a session object is created;
triggered when a session object is invalidated;

Servletrequestlistener

Requestinitialized (Servletrequestevent SRE);
Requestdestroyed (Servletrequestevent SRE);

The method is triggered before the HttpServletRequest object is passed to the service method of the user's servlet;

The method is triggered when the HttpServletRequest object is called after the service method of the user's servlet is invoked;


Note: Servletcontextlistener can no longer add new after the container is started, because the event it listens to will no longer appear.


Monitor the increase of attributes in three domain objects, delete, change the event listener

Listener class

The interface that contains

Interface description

Servletcontextattributelistener

attributeadded (servletcontextattributeevent scab);


Attributeremoved (servletcontextattributeevent scab);


attributereplaced (servletcontextattributeevent scab);

This method is triggered when the Servletcontext.setattribute method is invoked;


This method is triggered when the Servletcontext.removeattribute method is invoked;


If the attribute already exists before the call to Servletcontext.setattribute, this method is triggered when the attribute is replaced.

Httpsessionattributelistener

attributeadded (httpsessionbindingevent se);


attributeremoved (httpsessionbindingevent se);


attributereplaced (httpsessionbindingevent se);

The method is triggered when the Session.setattribute method is invoked;


The method is triggered when the Session.removeattribute method is invoked;


If the attribute already exists before the call to Session.setattribute, the method is triggered when the attribute is replaced;

Servletrequestattributelistener

attributeadded (Servletrequestattributeevent srae);


Attributeremoved (Servletrequestattributeevent srae);


attributereplaced (Servletrequestattributeevent srae);

This method is triggered when the Request.setattribute method is invoked;


This method is triggered when the Request.removeattribute method is invoked;


If the attribute already exists before the call to Request.setattribute, the method is triggered when the attribute is substituted;


Perceptual listeners

This listener does not need to be registered.

When a Java bean implements this interface, it can listen for when it is bound, untied, or deactivated.

Httpsessionbindinglistener: The class that implements this interface, can detect oneself when be httpsession bind, unbind;

Httpsessionactivationlistener: The class that implements this interface, can detect when oneself with httpsession activates and passivation together;


examples of listener applications

Like what:

Initialization of the

application--an application-level resource initialization in the Contextinitialized method of a custom inherited Servletcontextlistener listener for increased efficiency, Frees the application-level resources in the Contextdestroyed method ;

Related Article

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.