@WebListener Annotation Example (Jakarta EE)

In this blog post, we'll explore the @WebListener annotation, which is used to declare a listener in a web application. A listener is an object that gets notified when certain events occur in a web application, such as when the application starts or stops, when a session is created or destroyed, and so on.

The @WebListener annotation is part of the Jakarta Servlet API and is used to annotate a class to declare it as a listener. This annotation eliminates the need to configure listeners in the web.xml file.

Step 1: Add Servlet Dependency to pom.xml

First, we need to add the Jakarta Servlet dependency to our pom.xml file. Here, we are using version 6.1.0.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <name>java-servlet-tutorial Maven Webapp</name>
        <!-- Jakarta Servlet dependency -->

Step 2: Create a Listener Using @WebListener

Let's create a simple listener that listens to context initialization and destruction events.


package net.javaguides.servlet.tutorial.httpservlet;

import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;

public class AppContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Web application started");
        // Additional initialization code

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Web application stopped");
        // Additional cleanup code

In this example, the AppContextListener class implements the ServletContextListener interface and is annotated with @WebListener. The contextInitialized method is called when the web application is starting up, and the contextDestroyed method is called when the web application is shutting down.

Additional Examples of @WebListener

Session Listener

You can also create listeners for session events using the @WebListener annotation.

package net.javaguides.servlet.tutorial.httpservlet;

import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

public class AppSessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("Session created with ID: " + se.getSession().getId());

    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("Session destroyed with ID: " + se.getSession().getId());

In this example, the AppSessionListener class implements the HttpSessionListener interface and is annotated with @WebListener. The sessionCreated method is called when a new session is created, and the sessionDestroyed method is called when a session is invalidated or timed out.

Commonly Used Listener Interfaces

  • ServletContextListener: Receives notification events about ServletContext lifecycle changes.
  • HttpSessionListener: Receives notification events about HTTP session lifecycle changes.
  • ServletRequestListener: Receives notification events about ServletRequest lifecycle changes.


In this blog post, we demonstrated the usage of the @WebListener annotation to declare listeners in a web application. We covered the basic usage and provided additional examples to illustrate how to create listeners for different types of events. The @WebListener annotation simplifies listener configuration by allowing listeners to be defined directly in the Java class.


Related Servlet Posts