how to check session has been expired in java?

2022-09-03 16:30:41

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

session.isNew()


答案 1

Yes:

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

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


答案 2

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

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

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

You can put this code in a Filter.


推荐