如何在java中检查会话是否已过期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2070179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check session has been expired in java?
提问by user241924
Is there any other way to check expiry of session other than this
除了这个,还有没有其他方法可以检查会话的到期时间
session.isNew()
session.isNew()
回答by Carl Smotricz
Yes:
是的:
you can call
HttpServletRequest.getSession(false)
and you'll get anull
instead of a session if there isn't one active already.you can define a lifecycle listener (using
HttpSessionListener
) in yourweb.xml
. That way you can get notified the moment a session bites the dust.
你可以打电话
HttpServletRequest.getSession(false)
,null
如果还没有活动的话,你会得到一个而不是一个会话。您可以
HttpSessionListener
在web.xml
. 这样您就可以在会话尘埃落定的那一刻得到通知。
回答by Danilo Piazzalunga
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:
随着session.isNew
你无法区分全新的会话过期会话。您可以检查会话是否已过期和/或超时:
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.
使用getRequestedSessionId
新的和现有的(有效/过期)的会话,并使用区分isRequestedSessionIdValid
区分betwheen有效的和新的/过期的会话。
You can put this code in a Filter.
您可以将此代码放在Filter 中。