The @WebListener annotation is used to annotate a listener to get events for various operations on the particular web application context.
Below diagram shows a class diagram od @WebListener annotation which contains a single attribute value:
The classes annotated with @WebListener must implement one of the following interfaces:
- javax.servlet.ServletContextListener
- javax.servlet.ServletContextAttributeListener
- javax.servlet.ServletRequestListener
- javax.servlet.ServletRequestAttributeListener
- javax.servlet.http.HttpSessionListener
- javax.servlet.http.HttpSessionAttributeListener
- javax.servlet.http.HttpSessionIdListener
Let's add servlet 4.0.1 dependency to pom.xml:
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
@WebListener Annotation Example
The following example code uses the @WebListener annotation to register a class as a listener for the ServletContextListener’s events:
package net.javaguides.servlet.tutorial.listener;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Web application lifecycle listener.
*/
@WebListener
public class SimpleServletListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Context destroyed");
}
}
The following example code registers a listener which implements two interfaces with description:
package net.javaguides.servlet.tutorial.listener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Web application lifecycle listener.
*/
@WebListener()
public class SimpleServletListener implements ServletContextListener, ServletContextAttributeListener {
static final Logger log = Logger.getLogger(SimpleServletListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Context initialized");
log.info("Context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Context destroyed");
log.info("Context destroyed");
}
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
log.log(Level.INFO, "Attribute {0} has been added, with value: {1}",
new Object[] {
event.getName(), event.getValue()
});
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
log.log(Level.INFO, "Attribute {0} has been removed", event.getName());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
log.log(Level.INFO, "Attribute {0} has been replaced, with value: {1}",
new Object[] {
event.getName(), event.getValue()
});
}
}
Related Servlet Examples
- Servlet Life Cycle
- Servlet Interface Example
- GenericServlet Class Example
- HttpServlet Class Example Tutorial
- HttpServlet doGet() Method Example
- HttpServlet doPost() Method Example
- ServletConfig Interface Example
- @WebServlet Annotation Example
- @WebInitParam Annotation Example
- @WebListener Annotation Example
- @WebFilter Annotation Example
- @MultipartConfig Annotation Example
Check out complete Servlet 4 tutorial at Servlet Tutorial
Reference
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course