14

Is there any other way to check expiry of session other than this

session.isNew()

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user241924
  • 4,340
  • 7
  • 27
  • 25

2 Answers2

31

Yes:

  • you can call HttpServletRequest.getSession(false) and you'll get a null instead of a session if there isn't one active already.

  • you can define a lifecycle listener (using HttpSessionListener) in your web.xml. That way you can get notified the moment a session bites the dust.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
25

With session.isNew you can not distinguish expired sessions from entirely new sessions. You can check if the session has expired and/or timed out with:

if (request.getRequestedSessionId() != null
        && !request.isRequestedSessionIdValid()) {
    // Session is expired
}

Use getRequestedSessionId to distinguish between new and existing (valid/expired) sessions, and use isRequestedSessionIdValid to distinguish betwheen valid and new/expired sessions.

You can put this code in a Filter.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75