javax.servlet.ServletRequestAttributeListener :-


ServletRequestAttributeListener interface gets notified each time a attribute is added, removed or updated in ServletRequest. You can use it to know when an attribute is added, removed or updated from request.
It has following methods -

1- attributeAdded() - This method invokes when a attribute is added in ServletRequest. It has the following syntax -
public void attributeAdded(ServletRequestAttributeEvent event)        

2- attributeRemoved() - This method invokes when a attribute is removed from ServletRequest. It has the following syntax -
public void attributeRemoved(ServletRequestAttributeEvent event)        

3- attributeReplaced() - This method invokes when a attribute is replaced (updated) in ServletRequest. It has the following syntax -
public void attributeReplaced(ServletRequestAttributeEvent event)        

ServletRequestAttributeEvent -

javax.servlet.ServletRequestAttributeEvent is an event class corresponding to ServletRequestAttributeListener. It sends notification whenever any attribute added, removed or replaced from ServletRequest.
It has the following methods -

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

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


Example -

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

1 - index.html or index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>ServletRequsetAttributeListener 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 ServletRequestAttributeListener</h1>");
           request.setAttribute("Email","tom@gettechnotes.com");  // Add attribute in request
           request.setAttribute("Email", "jerry@gettechnotes.com");  // update attribute value from request
           request.removeAttribute("Email");    // Remove attribute from request
       }
       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 Request Attribute Listener -

package listeners;

import javax.servlet.*;

public class MyListener_1 implements ServletRequestAttributeListener {
   
    public void attributeAdded(ServletRequestAttributeEvent event) 
    {
        System.out.println("Attribute with name = "+ event.getName()+ " and value = "+ event.getValue()+ "  is added");
    }
 
    public void attributeReplaced(ServletRequestAttributeEvent event) {
       System.out.println("Attribute with name = "+ event.getName()+ "and value = "+ event.getValue()+ "  is replaced");
    }
    
    public void attributeRemoved(ServletRequestAttributeEvent 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 request, that causes ServletRequestAttributeListener methods to invoke.