HttpSessionAttributeListener Example Use Case

This example explains how to use a HttpSessionAttributeListener in a web application. This interface is used for receiving notification events about HttpSession attribute changes. The HttpSessionAttributeListener 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.

HttpSessionAttributeListener receives notifications for HttpSession attribute changes

In order to listen to a attribute change in the HttpSession we need to implement the javax.servlet.HttpSessionAttributeListener 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.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
public class SessionAttributeListener implements HttpSessionAttributeListener {

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        String sessionId = event.getSession().getId();
        String attributeName = event.getName();
        Object attributeValue = event.getValue();
        System.out.println("Attribute added : " + attributeName + " : " + attributeValue);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        String attributeName = event.getName();
        Object attributeValue = event.getValue();
        System.out.println("Attribute removed : " + attributeName + " : " + attributeValue);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        String attributeName = event.getName();
        Object attributeValue = event.getValue();
        System.out.println("Attribute replaced : " + attributeName + " : " + attributeValue);
    }
}

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.SessionAttributeListener</listener-class>
    </listener>
    
</web-app>

How does it work

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

References

You may also like...