javax.servlet.http.HttpSessionAttributeListener :-


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

1- attributeAdded() - This method invokes when a attribute is added to session. It has the following syntax -
public void attributeAdded(HttpSessionBindingEvent event)        

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

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


HttpSessionBindingEvent -

javax.servlet.http.HttpSessionBindingEvent is an event class corresponding to HttpSessionAttributeListener. It sends notification whenever any attribute is added (bound), removed (unbound) or replaced from session.
It has the following methods -

1- getSession() - It returns the session object. It has the following syntax -
public HttpSession getSession()        

2- getName() - It returns the name of the attribute that is bounded (added) or unbounded (removed) from session. It has the following syntax -
public String getName()        

3- getValue() - It returns the value of the attribute that is bounded (added) or unbounded (removed) or updated from session. It has the following syntax -
public Object getValue()        


Example -

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

1 - index.html or index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>HttpSessionAttributeListener 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 HttpSessionAttributeListener</h1>");
           
           HttpSession session = request.getSession();
           session.setAttribute("Email","tom@gettechnotes.com");   // Add attribute to session
           session.setAttribute("Email", "jerry@gettechnotes.com");  // update attribute value from session
           session.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 HttpSessionAttributeListener -
package listeners;

import javax.servlet.*;
import javax.servlet.http.*;

public class MyListener_1 implements HttpSessionAttributeListener {
   
    public void attributeAdded(HttpSessionBindingEvent event) 
    {
        System.out.println("Attribute with name = "+ event.getName()+ " and value = "+ event.getValue()+ "  is added");
    }
 
    public void attributeReplaced(HttpSessionBindingEvent event) {
       System.out.println("Attribute with name = "+ event.getName()+ "and value = "+ event.getValue()+ "  is replaced");
    }
    
    public void attributeRemoved(HttpSessionBindingEvent 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.