HttpSessionActivationListener Example Use Case

This example explains how to use a HttpSessionActivationListener in a web application. Objects that are bound to the HttpSession may listen to container events notifying them that sessions will be passivated and that session will be activated. This basically means that the session is migrated from one VM to another. A container that migrates sessions between VMs must invoke the corresponding events when an object implements the HttpSessionActivationListener.

HttpSessionActivationListener notifies if a session is migrated to another VM

In order for an Object to be notified when it will be migrated to another VM the Object needs to implement the HttpSessionActivationListener interface. This interface lets us listen to the following events.

  • sessionWillPassivate(): This event is raised when the object is about to be migrated to another VM.
  • sessionDidActivate(): This event is raised
package com.memorynotfound;

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

public class Dog implements HttpSessionActivationListener, Serializable {

    private String name;
    private String breed;

    public Dog(String name, String breed) { = name;
        this.breed = breed;

    public void sessionDidActivate(HttpSessionEvent event) {
        System.out.println("Dog: " + this.getName() + " is activated on a new JVM");

    public void sessionWillPassivate(HttpSessionEvent event) {
        System.out.println("Dog: " + this.getName() + " will be migrated to a new JVM");

    public String getBreed() {
        return breed;

    public String getName() {
        return name;

How does it work

When the container decides to migrate the session to another VM, it must call the sessionWillPassivate() method. This indicates that the object will be serialized and transported to another JVM. When the process of migration is complete, the sessionDidActivate() method is invoked. Telling the Object that it has migrated from one VM to another.


You may also like...