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


AsyncEvent 


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. 

ServletRequestEvent


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

ServletContextEvent


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

ServletRequestAttributeEvent


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

ServletContextAttributeEvent


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

HttpSessionEvent


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

HttpSessionBindingEvent


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


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

ServletContextListener


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

ServletContextAttributeListener 


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

ServletRequestListener


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

ServletRequestAttributeListener 


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

HttpSessionListener


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

HttpSessionBindingListener 


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

HttpSessionAttributeListener


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