How to check if a session is invalid

iddober picture iddober · Mar 11, 2009 · Viewed 41.3k times · Source

How to check if a session is invalid or not? There is no method in the API.

Is it the same as isNew()? And what is the difference if not?

Answer

John Wagenleitner picture John Wagenleitner · Mar 11, 2009

If you want to know whether it valid based on a request:

request.isRequestedSessionIdValid()

  or

HttpSession sess = request.getSession(false);
if (sess != null) {
   // it's valid
}

If you have stored a reference to the session and need to validate I would

try {
  long sd = session.getCreationTime();
} catch (IllegalStateException ise) {
  // it's invalid
}