Listener for Session Expiration


How do I listen for a session expiring? I have an application that has users, and I would like to log when they are ‘logged off’ (by their session expiring).

Override Application.close() - it will get called when the application end (either explicitly or by session expiration).

Application.close() is called when the application session ends either by session expiration or for another reason (ending it from the code). You could override it and do the logging there.

Jonas and Artur,

Thanks for the quick response.


Is the session already ended when close() is called? That is, can I retrieve a session object (the user) or is it already gone by the time I get this call?

Should be possible.

I’m doing a project in Vaadin 7. In that I need to hide my UI(or disable access to UI) when a user session expires.

I tried overriding the

public void close() {
    // Stuff to disable UI components

But, Nothing has happened. Is it possible to disable(freeze) the components in my UI automatically after session expires.

import com.vaadin.server.VaadinSession;
import java.util.Enumeration;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionExpireListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent hse) {
        System.out.println("Session created");

    public void sessionDestroyed(HttpSessionEvent hse) {
        System.out.println("Session destroyed");
        Enumeration e = hse.getSession().getAttributeNames();
        while (e.hasMoreElements()) {
            Object o = hse.getSession().getAttribute((String) e.nextElement());
            if (o instanceof VaadinSession) {
                VaadinSession vs = (VaadinSession) o;

where should we add this SessionExpireListener?

IMHO, a much cleaner way to do this would be as described in the following article.

Updated link: