15 - Servlet Listeners: Page 2 of 4

15.2.2 ServletContextAttributeListener

ServletContextAttributeListener listens to SessionContexAttributetEvent event which gives a notification when any  object is added, removed or replaced from servlet context .

ServletContextAttributeListener  is the interface and it defines three methods –

  • attributeAdded(ServletContextAttributeEvent e): It notifies whenever a new  attribute is added to the servlet context.
  • attributeRemoved(ServletContextAttributeEvent e): It notifies whenever the attribute is removed from the servlet context.
  • attributeReplaced(ServletContextAttributeEvent e): It notifies whenever the attribute gets replaced on the servlet context.

Attribute name and value  that has been added, removed or replaced  can be obtained from the getName() and getValue() method of ServletContextAttributeEvent
Let s write a sample listener which will print some message describing the event and attribute values

  1. Add listener entry in web.xml
<listener>
 <description>Servlet Context Attribute Listener Example</description>
 <listener-class>com.servlet.tutorial.MyServletContextAttributeListener</listener-class>
</listener>
  1. Write MyServletContextAttributeListener code
package com.servlet.tutorial;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    /**
     * Default constructor. 
     */
    public MyServletContextAttributeListener() {
    }
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("Attribute has been added");
        String attributeName = servletContextAttributeEvent.getName();
        Object attributeValue = servletContextAttributeEvent.getValue();
        System.out.println("Attribute Name ::" + attributeName);
        System.out.println("Attribute Value ::" + attributeValue.toString());
    }
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("Attribute has been replaced");
        String attributeName = servletContextAttributeEvent.getName();
        Object attributeValue = servletContextAttributeEvent.getValue();
        System.out.println("Attribute Name ::" + attributeName);
        System.out.println("Attribute Value ::" + attributeValue.toString());
    }
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("Attribute has been removed");
        String attributeName = servletContextAttributeEvent.getName();
        Object attributeValue = servletContextAttributeEvent.getValue();
        System.out.println("Attribute Name ::" + attributeName);
        System.out.println("Attribute Value ::" + attributeValue.toString());
    }
}
  1. Add ServletContextAttributeListenerDemo servlet entry in web.xml
<servlet>
    <servlet-name>ServletContextAttributeListenerDemo</servlet-name>
    <servlet-class>com.servlet.tutorial.ServletContextAttributeListenerDemo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletContextAttributeListenerDemo</servlet-name>
    <url-pattern>/ServletContextAttributeListenerDemo</url-pattern>
  </servlet-mapping>

  1. Write ServletContextAttributeListenerDemo servlet code which will add , replace and remove the attribute
package com.servlet.tutorial;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextAttributeListenerDemo extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        ServletContext ctx = getServletContext();
        String attributeName="ContextAttribute";
        String attributeValue="Initial Value";
        ctx.setAttribute(attributeName, attributeValue);
        attributeValue="New Value";
        ctx.setAttribute(attributeName, attributeValue);
        ctx.removeAttribute(attributeName);
    }
}

Testing

Hit http://localhost:8080/HelloWorld/ServletContextAttributeListenerDemo

Note- getValue() returns the  previous value in case of replace because listener is executed before it is actually replaced.

Like us on Facebook