ServletContextAttributeListener Example

ServletContextAttributeListener Example Tutorial explains about how to use ServletContextAttributeListener in a web application.

The listener ServletContextAttributeListener is an interface extending from base interface java.util.EventListener.

ServletContextAttributeListener  will be notified when an attribute is changed on a particular servletcontext

ServletContextAttributeListener interface has following methods:

attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)

Package Structure

ServletContextAttributeListener Example

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <listener>
        <listener-class>com.listener.ServletContextAttributeListenerExample</listener-class>
    </listener>
</web-app>

ServletContextAttributeListenerExample.java

package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class ServletContextAttributeListenerExample implements ServletContextAttributeListener {

 
/*
   * This method is invoked when an attribute is added to the ServletContext object
   */
 
public void attributeAdded(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was added to the ServletContext object");
 
}

 
/*
   * This method is invoked when an attribute is removed from the ServletContext object
   */
 
public void attributeRemoved(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was removed from the ServletContext object");
 
}

 
/*
   * This method is invoked when an attribute is replaced in the ServletContext object
   */
 
public void attributeReplaced(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was replaced in the ServletContext object");
 
}

}
Server Console Output
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 29, 2011 5:47:56 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 29, 2011 5:47:56 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1246 ms
Nov 29, 2011 5:47:56 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 29, 2011 5:47:56 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.21
Nov 29, 2011 5:47:56 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [140] milliseconds.
An attribute was added to the ServletContext object
Note

From Servlet API 3.0, we can use javax.servlet.annotation, so that web.xml is not necessary

Here we are using @WebListener annotation

ServletContextAttributeListenerExample

package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ServletContextAttributeListenerExample implements ServletContextAttributeListener {

 
/*
   * This method is invoked when an attribute is added to the ServletContext object
   */
 
public void attributeAdded(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was added to the ServletContext object");
 
}

 
/*
   * This method is invoked when an attribute is removed from the ServletContext object
   */
 
public void attributeRemoved(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was removed from the ServletContext object");
 
}

 
/*
   * This method is invoked when an attribute is replaced in the ServletContext object
   */
 
public void attributeReplaced(ServletContextAttributeEvent scab) {
   
System.out.println("An attribute was replaced in the ServletContext object");
 
}

}

Tomcat Console Output

An attribute was added to the ServletContext object








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