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.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!