RE: Check if current Session is still open


According to the getState() method javadoc, it returns the lifecycle state of the session, so you are using it correctly.
The question is, where do you pefrom the check and how do you already know that the session has been closed?

– Goran

Hi Alex,

How do you know that the session has already been closed? Also how do you call the sessionStillOpen method?

I’d go with a different approach that gurantees notification once the session is destroyed, which is basically addding a Session Destroy Listener:

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {

  public void init() throws ServletException {

    getService().addSessionDestroyListener(evt -> {
      // The session is dead

Hi Alex,

I recommend you read up on
Applicaiton Lifecycle
from the docs, with emphasis on the “Handling Session Initialization and Destruction” and “Session Expiration” sections.
Also if you just want to setup a redirect URL when the session ends, you can take a look at
Customizing System Messages

Hope this helps.


Is there a way to check if a session is still open?

I tried via, private boolean sessionStillOpen() { return VaadinSession.getCurrent().getState() == State.OPEN; } but it returns true although the session has already been closed.

Am I using getState() the wrong way?

Okay it’s on me. My backend authentication dies before the vaadin session, that’s why the method returns true.

I was using that method in an other method that was assigned to a button that was showing just a view and I wanted to just redirect people in case the session ended.

I didn’t know about customizing system messages! That’s so cool!