javax.servlet.ServletContextAttributeListener :-


ServletContextAttributeListener interface gets notified each time an attribute added, removed or replaced from ServletContext(application). You can use it to know when a application attribute is added, removed or replaced.
It has following methods -

1- attributeAdded() - This method invokes when a attribute is added to ServletContext. It gets called after attribute is added to servlet context. It has the following syntax -
public void attributeAdded(ServletContextAttributeEvent event)        

2- attributeRemoved() - This method invokes after the attribute is removed from ServletContext. It has the following syntax -
public void attributeRemoved(ServletContextAttributeEvent event)        

3- attributeReplaced() - This method invokes after the attribute has replaced (update) from ServletContext. It has the following syntax -
public void attributeReplaced(ServletContextAttributeEvent event)        


ServletContextAttributeEvent -

javax.servlet.ServletContextAttributeEvent is an event class corresponding to ServletContextAttributeListener. It sends notification whenever any attribute is added or replaced or removed from ServletContext.
It has the following methods -

1- getName() - It returns the name of the attribute that is added or replaced or removed from ServletContext. It has the following syntax -
public String getName()        

2- getValue() - It returns the value of the attribute that is added or replaced or removed from ServletContext. It has the following syntax -
public Object getValue()        


Example -

This simple example will show you how to create and use ServletContextAttributeListener.

1 - index.html or index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>ServletContextAttributeListener Demo</title>
    </head>
    <body>
        <a href="First">Click Here</a>
    </body>
</html>  

2- Create a package named serv and within that create a servlet named First.
First Servlet -
package servs;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class First extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       try
       {
           PrintWriter out = response.getWriter();
           out.println("<h1>This is just a demo of ServletContextAttributeListener</h1>>");
           
           ServletContext application = getServletConfig().getServletContext();
           application.setAttribute("Email","tom@gettechnotes.com");   // Add attribute to session
           application.setAttribute("Email", "jerry@gettechnotes.com");  // update attribute value from session
           application.removeAttribute("Email");    // Remove attribute from session
       }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
    }
}

3- Create a package named listeners and within that create a class named MyListener_1 that will work as a ServletContextAttributeListener -

package listeners;

import javax.servlet.*;

public class MyListener_1 implements ServletContextAttributeListener 
{
    public void attributeAdded(ServletContextAttributeEvent event) 
    {
      System.out.println("Attribute with name = "+ event.getName()+ " and value = "+ event.getValue()+ "  is added");
    }

    public void attributeReplaced(ServletContextAttributeEvent event) 
    {
      System.out.println("Attribute with name = "+ event.getName()+ "and value = "+ event.getValue()+ "  is replaced");  
    }
    
    public void attributeRemoved(ServletContextAttributeEvent event) 
    {
      System.out.println("Attribute with name = "+ event.getName()+ "and value = "+ event.getValue()+ "  is removed");
    }
}


4- web.xml - Add the information of servlet and listener in web.xml file -
<web-app>
    <listener>
        <listener-class>listeners.MyListener_1</listener-class>
    </listener>
    <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>servs.First</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/First</url-pattern>
    </servlet-mapping>
    </web-app>


5- Now run the project, It will show you index page -



Click on the given link and it will run the First Servlet, that add replace and remove an attribute from session that causes HttpSessionAttributeListener methods to invoke.