Java.Servlet.How can we notify an object in a session that the session is invalid or expired?

Short answer:
You can notify an object in the session about session invalidation or expiration by making the object implement the HttpSessionBindingListener or HttpSessionActivationListener interface.

These special interfaces allow session attributes (objects stored inside the session) to get “events” when something happens!

Two main ways:

InterfacePurpose
HttpSessionBindingListenerNotified when the object is added to or removed from a session (including when session is invalidated).
HttpSessionActivationListenerNotified when the session is passivated (serialized to disk) or activated (restored) — more for distributed systems or clustering.

👉 For session expiration/invalidation, the important one is HttpSessionBindingListener.

How to do it:

  1. Your object (that you store in the session) needs to implement HttpSessionBindingListener.
  2. Then it must override two methods:
void valueBound(HttpSessionBindingEvent event);
void valueUnbound(HttpSessionBindingEvent event);

Full Example:

import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionBindingEvent;

public class MySessionObject implements HttpSessionBindingListener {

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("Object bound to session: " + event.getSession().getId());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("Session is ending. Cleaning up for session: " + event.getSession().getId());
    }
}

Now, if you do:

HttpSession session = request.getSession();
session.setAttribute("myObject", new MySessionObject());

Typical usage:
In valueUnbound() you can:

  • Free resources (e.g., database connections).
  • Save final data.
  • Send final logs.
  • Notify some service.

Important note:

  • This only works if the object is still in the session at the time of invalidation.
  • If you manually remove it earlier (session.removeAttribute()), valueUnbound() is also called.

Related:

  • If you need global notifications about any session, not just a specific object, then you use a HttpSessionListener at the servlet container level. (It listens for session creation and destruction.)

🔥 Super quick cheat sheet:

What you wantWhat you use
When an object is added or removed from sessionHttpSessionBindingListener
When session itself is created or destroyed (global)HttpSessionListener
When session is passivated/activated (for clustering)HttpSessionActivationListener

✨ Real-world metaphor:

Imagine you are a guest in a hotel (session).

  • You (the object) are notified when you check-in (valueBound) and when you check-out (valueUnbound).

Summary:

  • To notify an object about session expiration/invalidation, make it implement HttpSessionBindingListener.
  • Override valueUnbound().
  • It will be called automatically when the session ends or the object is removed!

This entry was posted in Без рубрики. Bookmark the permalink.