Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Understanding Session Listeners in Java Servlet Applications

Notes 3

Session listeners in Java Servlet applications provide mechanisms to monitor and respond to events related to HTTP sessions. There are four primary listener interfaces:

  1. HttpSessionListener
  2. HttpSessionAttributeListener
  3. HttpSessionBindingListener
  4. HttpSessionActivationListener

Each interface serves specific purposes in tracking session lifecycle and attribute changes.

HttpSessionListener and HttpSessionAttributeListener

These listeners monitor session creation/destruction and attribute modifications across all session in the application.

package com.example.listeners;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class SessionMonitor implements HttpSessionListener, HttpSessionAttributeListener {
    
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("New session created");
    }
    
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.println("Session destroyed");
    }
    
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("Attribute added to session");
    }
    
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("Attribute removed from session");
    }
    
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("Attribute modified in session");
    }
}

HttpSessionBindingListener

This listener monitors binding events for specific session objects. Unlike HttpSessionListener which monitors all sessions, HttpSessionBindingListener tracks individual objects bound to sessions.

package com.example.listeners;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/*
 * HttpSessionListener registers globally via @WebListener or web.xml
 * HttpSessionBindingListener requires explicit binding via setAttribute()
 */
public class ObjectBindingTracker implements HttpSessionBindingListener {
    
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("Object bound to session");
    }
    
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("Object unbound from session");
    }
}

// Usage example:
// session.setAttribute("tracker", new ObjectBindingTracker());

HttpSessionActivationListener

This listener handles session passivation and activation events, typically used in distributed environments where sessions might be serialized and deserialized.

package com.example.listeners;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionStateMonitor implements HttpSessionActivationListener {
    
    @Override
    public void sessionWillPassivate(HttpSessionEvent event) {
        System.out.println("Session about to be passivated");
    }
    
    @Override
    public void sessionDidActivate(HttpSessionEvent event) {
        System.out.println("Session activation completed");
    }
}

Key Differences

  • HttpSessionListener: Monitors all session lifecycle events (creation/destruction)
  • HttpSessionAttributeListener: Tracks attribute changes across all sessions
  • HttpSessionBindingListener: Monitors binding/unbinding of specific objects to sessions
  • HttpSessionActivationListener: Handles serializaiton/deserialization events in distributed sessions

These listeners enable developers too implement session management features, monitoring, and cleanup operations in web applications.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Spring Boot MyBatis with Two MySQL DataSources Using Druid

Required dependencies application.properties: define two data sources and poooling Java configuration for both data sources MyBatis mappers for each data source Controller endpoints to verify both co...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.