ServletRequestAttributeListener Example Use Case

This example explains how to use a ServletRequestAttributeListener in a web application. This interface is used for receiving notification events about ServletRequest attribute changes. Notifications will be generated while the request is within the scope of the web application in which the listener is registered. A ServletRequest has a very short lifespan. It only lives when it enters the first servlet or filter and is destroyed when it reaches the last servlet or filter. The ServletRequestAttributeListener can be registerd by @WebListener annotation, adding the listener to the servlet descriptor or programmatically adding a listener with .addListener() to the servlet context. In this example we use the @WebListener annotation.

ServletRequestAttributeListener receives notifications for ServletRequest attribute changes

In order to listen to a attribute change in the ServletContext we need to implement the javax.servlet.ServletRequestAttributeListener interface. This interface lets us listen to the following events, the names speak for themselves.

  • attributeAdded()
  • attributeRemoved()
  • attributeReplaced()

To register a listener we can add the @WebListener, define the listener in the servlet descriptor (web.xml) or programatigally add it to the servlet context. In this example we choose to add the listener through the @WebListener annotation.

package com.memorynotfound;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class ServletRequestAttributeLogger implements ServletRequestAttributeListener {

    @Override
    public void attributeAdded(ServletRequestAttributeEvent event) {
        System.out.println("attribute: " + event.getName() + " was added with value: " + event.getValue());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent event) {
        System.out.println("attribute: " + event.getName() + " was added with value: " + event.getValue());
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent event) {
        System.out.println("attribute: " + event.getName() + " was added with value: " + event.getValue());
    }
}

Note: If you prefer the web.xml servlet descriptor over the @WebListener annotation you can add the context listener as follows:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <listener>
        <listener-class>com.memorynotfound.ServletRequestAttributeLogger</listener-class>
    </listener>
    
</web-app>

How does it work

Every time an ServletRequest attribute is added, replaced or removed the corresponding method is invoked. This allows us to track changes to certain attributes in our ServletRequest.

References

You may also like...