Servlet Listeners and Events

Listeners are classes that will wait for certain life cycle events and the container will invoke certain methods of these classes when that life cycle event happens. There will be listener interfaces, with methods that need to be invoked when a life cycle event occur. The listener implementation class will need to implement this interface.

You can specify a listener in either of the ways:

  • @WebListener annocation over the listener implementation class

  • By configuring in the deployment descriptor (web.xml). In the deployment descriptor, you need to place a <listener> element under the root element, and within the <listener> element, embed a <listener-class> element. The value held in the <listener-class> element should be the fully qualified class name of a listener class. There is no need to specify which type of listener you’re talking about, as the web container will find this out using Java’s reflection capabilities. 

 

We have not covered topics required for some of the listeners mentioned here; you can come back and look into those listeners once those topics are covered.

 

Listeners interfaces for request object

 

ServletRequestListener

  • Responds to the life and death of each request.  

Methods are:

  • public void requestInitialized(ServletRequestEvent requestEvent)

    • Called at the beginning of any request’s scope. This is either at the start of the servlet’s service() method or at the start of the doFilter() method for the first filter in a chain.  

  • public void requestDestroyed(ServletRequestEvent requestEvent)

    • Called for each request that comes to an end—either at the end of the servlet’s service() method or at the end of the doFilter() method for the first filter in a chain. 

 

ServletRequestAttributeListener

  • Responds to any change to the set of attributes attached to a request object.

Methods are:

  • attributeAdded(ServletRequestAttributeEvent srae)  

    • Called whenever a new attribute is added to any request (as a result of any call to ServletRequest.setAttribute() for a new attribute).

  • attributeRemoved(ServletRequestAttributeEvent srae)  

    • Called whenever an attribute is removed from a request (as a result of any call to ServletRequest.removeAttribute()).

  • attributeReplaced(ServletRequestAttributeEvent srae)  

    • Called whenever an attribute is replaced (as a result of any call to ServletRequest.setAttribute() for an attribute name already in use on that request). 

 

Note: Responses don't have or need listeners.

 

Listener interfaces for context object

 

ServletContextListener

  • Responds to the life and death of the context for a web application.

Methods are:

  • contextInitialized(ServletContextEvent sce)

    • Called at the beginning of context scope. Gets called before any servlet’s init() method or any filter’s doFilter() method.  

  • contextDestroyed(ServletContextEvent sce)

    • Called at the end of context scope. And every filter and servlet destroy() method must have executed before the contextDestroyed() method is called. 

 

ServletContextAttributeListener

  • Responds to any change to the set of attributes attached to the context object.

Methods are:

  • attributeAdded(ServletContextAttributeEvent scae)

  • attributeRemoved(ServletContextAttributeEvent scae)

  • attributeReplaced(ServletContextAttributeEvent scae) 

 

Listener interfaces for session object

 

HttpSessionListener

  • Responds to the life and death of the session.

Methods are:

  • sessionCreated(HttpSessionEvent event)

    • Called by the web container whenever a new session is provided.

  • sessionDestroyed(HttpSessionEvent event)  

    • Called by the web container at the moment a session is about to be invalidated—within the call to HttpSession.invalidate(), but before the session becomes invalid and unusable.

 

HttpSessionAttributeListener

  • Responds to any change to the set of attributes attached to the session object.

Methods are:

  • attributeAdded(HttpSessionBindingEvent hsbe)  

    • Called whenever a new attribute is added to any session.

  • attributeRemoved(HttpSessionBindingEvent srae)  

    • Called whenever an attribute is removed from any session

  • attributeReplaced(HttpSessionBindingEvent srae)  

    • Called whenever an attribute is replaced 

 

HttpSessionBindingListener  

  • HttpSessionBindingListener interface should be implemented by classes that may be bound into sessions as attributes and if they want to know when they were bound.

  • When an object which implements this interface is bound to a session or unbound from the session, the container will call its valueBound and valueUnbound methods respectively.

  • Since the interface is added by the attributes that are added to the session themselves and cannot listen to any other attributes, you should not mention it in the web.xml file or have the @WebListener annotation. For more details, refer to http://javajee.com/demo-httpsessionbindinglistener-an-object-knowing-whe....

  • The valueBound and valueUnbound methods of this interface are passed with a HttpSessionBindingEvent event that contains the below important methods:

    • getName – Name of the attribute added to session.

    • getValue – Value of the attribute will be this object itself.

    • GetSession – Current session object.

    • It also contain the inherited methods from EventObject: getSource() and toString().

  • HttpSessionBindingEvent is sent to methods of both objects that implements HttpSessionBindingListener as well as HttpSessionAttributeListener.

Methods are:

  • valueBound(HttpSessionBindingEvent hsbe)  

    • Called whenever the object implementing the HttpSessionBindingListener interface is the value object passed to an HttpSession.setAttribute() call.

  • valueUnbound(HttpSessionBindingEvent hsbe)  

    • Called whenever the object implementing the HttpSessionBindingListener interface is removed from the session as a result of an HttpSession.removeAttribute() call. 

 

HttpSesssionActivationListener  

  • Receives events when a value object is transported across JVMs. This happens when the object is an attribute of a session in a distributed environment. 

Methods are:

  • sessionWillPassivate(HttpSessionEvent hse)

    • Called on each HttpSessionActivationListener implementing object bound to the session just prior to the serialization of the session (and all its attributes).  

  • sessionDidActivate(HttpSessionEvent hse)

    • Called on each HttpSessionActivationListener implementing object bound to the session just after deserialization of the session (and all its attributes).

 

HttpSessionIdListener

  • Receives events when session id gets changed.

  • Added since Servlet 3.1 (Java EE 7)

  • The order in which implementations of this interface are invoked is unspecified.

Method is:

  • sessionIdChanged(HttpSessionEvent event, String oldSessionId

    • Receives event object and old session id when session id gets changed.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)