Servlet Listener is used for listening to events in a web container, such as when you create a session or place an attribute in a session or if you passivate and activate in another container, to subscribe to these events you can configure listener in web.xml, for example, HttpSessionListener.
Listeners get triggered for an actual physical request that can be attached to events in your app server .With listeners, you can track application-level, session-level, life-cycle changes, attribute changes etc.
You can monitor and react to events in a servlet’s life cycle by defining listener objects whose methods get invoked when lifecycle events occur.
Event is the occurrence of something, an event can be the initialization of the application, destroying an application, request from the client, creating/destroying a session, attribute modification in session etc.
This provides different types of Listener interfaces that we can implement and configure in web.xml.
Servlet API provides following event objects.
Event that gets fired when the asynchronous operation initiated on a ServletRequest has completed, timed out, or produced an error.
Events of this type are either sent to 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 session binds the object by a call to HttpSession.setAttribute and unbinds the object by a call to HttpSession.removeAttribute.
We can use this event for cleanup activities when object is removed from session.
Event notifications for changes to sessions within a web application.
Event notifications about changes to the attributes of the ServletContext of a web application
Event notifications about changes to the servlet context of a web application
Event notification about lifecycle of ServletRequest.
Event class for notifications of changes to the attributes of the servlet request in an application
Servlet API provides following Listener interfaces :-
Listener that will be notified in the event that an asynchronous operation initiated on a ServletRequest to which the listener had been added has completed, timed out, or resulted in an error
Interface for receiving notification events about ServletContext lifecycle changes
Interface for receiving notification events about ServletContext attribute changes
Interface for receiving notification events about requests coming into and going out of scope of a web application
Interface for receiving notification events about ServletRequest attribute changes
Interface for receiving notification events about HttpSession lifecycle changes
Causes an object to be notified when it is bound to or unbound from a session.
Interface for receiving notification events about HttpSession attribute changes
Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.
Example : Counting logged in users Using HttpSessionEvent and HttpSessionListener