Saturday, April 11, 2020

Events and Listeners

Events and Listeners

Events can be considered as an occurrence of something or when the change of the state happens to something, it is called an event. 

Several tasks can be executed upon the occurrences of the events, as in a web application, an event can be the initialization of the application, request from a client, creating/destroying a session, attribute update in a session, destroying an object, creating database connection object, etc. 

Two levels of servlet events and Listeners

There are two levels of the events and the Listeners,

Context level

The event-related to state or the resources available at the application or context (ServletContext object) level. 

Session level

The events at session-level, those are associated with the resources or state about the information of the requests from a single user session. This information is associated with the HTTP session objects. 

Both of these events can capture Lifecycle changes or Attribute changes. 

These event classes and Listener interfaces are provided in the javax.servlet and javax.servlet.http packages. 

Event classes

The event classes are listed as, 

Event classes servlets


These event are fired when an asynchronous operation is initiated on a ServletRequest by ServletRequest#startAsync or ServletRequest#startAsync(ServletRequest, ServletResponse).These events occur when an asynchronous operation is completed, time-out, or produces an error. 


These are lifecycle events for ServletRequest objects. The ServletContext is the source of the event in this web application. 


ServletContextEvent class is used for notifications about changes to the servlet context of a web application. 


ServletRequestAttributeEvent is the event class for the notifications of changes to the attributes of the servlet request in an application. 


ServletContextAttributeEvent event class is used to detect events about modifications in the attributes of a ServletContext of the web application. 


The events about the changes to HTTP sessions within a web application can be detected. 


These events are detected either upon sending an object that implements 
HttpSessionBindingListener, when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the web.xml when any attribute is bound, unbound or replaced in a session. 

The object is bounded to session by a call to HttpSession.setAttribute() and unbound by the to HttpSession.removeAttribute() methods. 

This event is used for the clean-up process when the object is erased from the HTTP session. 

Event Listeners

The event interfaces(Listeners) are listed as, 

servlet event listeners


AsyncListener notifies upon capturing an asynchronous operation initiated on a ServletRequest is complete, timed-out, or produced an error. 


This Listener is used for receiving notification events upon the detection of ServletContext lifecycle events. 


This Interface is used for listening notification events from the changes in ServletContext attributes. 


ServletRequestListener interface is used for capturing notification events about incoming and outgoing requests from the scope of a web application. 


This interface is used for capturing notification events regarding the changes in the ServletRequest attribute. 


The Listener is used for capturing the notification events regarding the modification in the HttpSession lifecycle. 


This Listener makes an object to be notified upon its bounding or un bounding from a session. 


This Interface is used for capturing the notification events if there are changes in the HttpSession attribute.