Session is invalidated or timed-out

Session is invalidated or timed-out

In a web application, sessions are used to store user-specific data across multiple requests. Sometimes, it is necessary to perform certain actions when a session is invalidated or times out. For instance, you might want to release resources or log user activity when the session ends.

To achieve this, you can use the HttpSessionBindingListener interface. This interface provides a way to receive notifications when an object is bound to or unbound from a session. Specifically, it has two methods:

  • valueBound(HttpSessionBindingEvent event): Called when the object is added to a session.
  • valueUnbound(HttpSessionBindingEvent event): Called when the object is removed from a session, including when the session is invalidated or times out.

By implementing the HttpSessionBindingListener interface in your object, you can perform the necessary actions when the session is invalidated or times out.

Session is invalidated

Example in Java

Here’s an example demonstrating how to notify an object when a session is invalidated or times out by implementing the HttpSessionBindingListener interface:

Step 1: Create a Class Implementing HttpSessionBindingListener

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

public class SessionListenerExample implements HttpSessionBindingListener {

    // Constructor or other methods can be added here as needed

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        // This method is called when the object is added to a session
        System.out.println("Object added to session: " + event.getSession().getId());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        // This method is called when the object is removed from a session or when the session is invalidated or timed out
        System.out.println("Object removed from session: " + event.getSession().getId());
        // Perform cleanup or other actions here
    }
}

In your servlet or other components, you can add an instance of the SessionListenerExample class to the session:

Step 2: Add the Object to the Session

java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        
        // Add an instance of SessionListenerExample to the session
        SessionListenerExample listenerExample = new SessionListenerExample();
        session.setAttribute("sessionListenerExample", listenerExample);
        
        response.setContentType("text/html");
        response.getWriter().println("SessionListenerExample added to session. Session ID: " + session.getId());
    }
}

With this setup, whenever the session is invalidated (e.g., via session.invalidate()) or times out, the valueUnbound method of the SessionListenerExample object will be called, allowing you to perform any necessary cleanup or actions.

Homepage

Readmore