tags:

views:

767

answers:

3

Hi, 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.

Thanks, Ido

A: 

For all intents and purposes, yes. However, it will throw an IllegalStateException if called on a session invalidated in the same request-response cycle.

Chry Cheng
+4  A: 

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
}
John Wagenleitner
You saved my day!! Any way checking for an exception looks like a odd way, shouldn't they provide an API method to check whether the session is valid??
Arun P Johny
A: 

isNew() is true only if this session wasn't yet accepted by client (i.e. it was just created, and JSESSIONID wasn't sent yet, or if it was sent, client didn't send it back, so server doesn't know about it and created another session)

Peter Štibraný