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. 

servlet listener


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.

Servlet API

This provides different types of Listener interfaces that we can implement and configure in web.xml.

Servlet API provides following event objects.

Event Object
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 Object
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


<form action="login">
Name:<input type="text" name="userName"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="Login"/>

import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
public class First extends HttpServlet {  
 public void doGet(HttpServletRequest request, 
       HttpServletResponse response)  
throws ServletException, IOException {  
    PrintWriter out = response.getWriter();  
  String username =request.getParameter("userName");  
    out.print("Welcome "+ userName);  
    HttpSession session=request.getSession();  
    ServletContext ctx=getServletContext();  
    int loggedInUsers=(Integer)ctx.getAttribute("loggedInUsers");  
out.print("<br>Total logged in users= "+loggedInUsers);  

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
ServletContext ctx=null;  
static int loggedInUsers=0;  
publicvoid sessionCreated(HttpSessionEvent sessionEvent){
 ctx.setAttribute("loggedInUsers", loggedInUsers);  
publicvoid sessionDestroyed(HttpSessionEvent sessionEvent){


<?xml version=“1.0” encoding=“UTF-8”?>
<display-name>Servlet Listener</display-name>

Reference :-

Leave Comment

Your email address will not be published. Required fields are marked *