Servlet Listeners

Servlet listeners are useful for tracking and responding to various lifecycle events in a web application. By using these listeners, you can monitor session creation, attribute changes, and request processing, enabling more control over the web application's behavior.

Servlet listeners implement specific listener interfaces provided by the Servlet API. These interfaces are categorized into the following:

  1. ServletContextListener – for application-wide events.
  2. ServletContextAttributeListener – for changes to attributes in the servlet context.
  3. HttpSessionListener – for session creation and destruction events.
  4. HttpSessionAttributeListener – for changes to session attributes.
  5. ServletRequestListener – for request creation and destruction events.
  6. ServletRequestAttributeListener – for changes to request attributes.

Example of Listeners

Let's go through a simple example that demonstrates different types of listeners in a servlet application.

1. ServletContextListener

This listener is used to track the lifecycle events of the ServletContext (web application), like when the application is initialized and destroyed.


import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContext Initialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContext Destroyed"); } }

2. HttpSessionListener

This listener is used to track the creation and destruction of HTTP sessions.

import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("Session Created: " + se.getSession().getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("Session Destroyed: " + se.getSession().getId()); } }

3. ServletRequestListener

This listener is used to track the creation and destruction of requests in the web application.

import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyServletRequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("Request Destroyed"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("Request Initialized"); } }

4. ServletContextAttributeListener

This listener is used to track changes to attributes in the ServletContext.


import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyServletContextAttributeListener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent event) { System.out.println("Attribute Added: " + event.getName() + " = " + event.getValue()); } @Override public void attributeRemoved(ServletContextAttributeEvent event) { System.out.println("Attribute Removed: " + event.getName()); } @Override public void attributeReplaced(ServletContextAttributeEvent event) { System.out.println("Attribute Replaced: " + event.getName() + " = " + event.getValue()); } }

5. HttpSessionAttributeListener

This listener is used to track changes to attributes within a session.


import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent event) { System.out.println("Session Attribute Added: " + event.getName() + " = " + event.getValue()); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { System.out.println("Session Attribute Removed: " + event.getName()); } @Override public void attributeReplaced(HttpSessionBindingEvent event) { System.out.println("Session Attribute Replaced: " + event.getName() + " = " + event.getValue()); } }

6. ServletRequestAttributeListener

This listener is used to track changes to attributes within a request.

import javax.servlet.*; import javax.servlet.annotation.WebListener; @WebListener public class MyServletRequestAttributeListener implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent event) { System.out.println("Request Attribute Added: " + event.getName() + " = " + event.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent event) { System.out.println("Request Attribute Removed: " + event.getName()); } @Override public void attributeReplaced(ServletRequestAttributeEvent event) { System.out.println("Request Attribute Replaced: " + event.getName() + " = " + event.getValue()); } }

Deployment in web.xml

While using annotations (like @WebListener) is the modern approach, you can also register listeners manually in the web.xml if annotations are not used

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- Register listeners --> <listener> <listener-class>com.example.MyServletContextListener</listener-class> </listener> <listener> <listener-class>com.example.MyHttpSessionListener</listener-class> </listener> <listener> <listener-class>com.example.MyServletRequestListener</listener-class> </listener> </web-app>

How It Works:

  • ServletContextListener: Invoked when the web application is initialized and destroyed.
  • HttpSessionListener: Invoked when HTTP sessions are created and destroyed.
  • ServletRequestListener: Invoked when an HTTP request is initialized and destroyed.
  • Attribute Listeners: They monitor changes (add, remove, replace) to attributes within the context, session, and request.

Comments