Java 如何检查会话是否无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/634929/
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 if a session is invalid
提问by iddober
采纳答案by John Wagenleitner
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
}
回答by Chry Cheng
For all intents and purposes, yes. However, it will throw an IllegalStateException if called on a session invalidated in the same request-response cycle.
出于所有意图和目的,是的。但是,如果在同一请求-响应周期中无效的会话上调用,它将抛出 IllegalStateException。
回答by Peter ?tibrany
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)
isNew()
仅当此会话尚未被客户端接受时才为真(即它刚刚创建,并且 JSESSIONID 尚未发送,或者如果已发送,客户端没有将其发送回来,因此服务器不知道它并创建了另一个会话)