Java 会话超时混淆 - session.setMaxInactiveInterval(0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21064035/
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
Session timeout confusion - session.setMaxInactiveInterval(0)
提问by JEENoob
I am new to JEE and this is what confuses me. According to HttpSession.html#setMaxInactiveInterval(int interval)documentation
我是 JEE 的新手,这让我感到困惑。根据HttpSession.html#setMaxInactiveInterval(int interval)文档
An
interval
value ofzero
or less indicates that the session should never timeout.
或小于的
interval
值zero
表示会话不应超时。
but according to my text book (which already is few years old - so I expect it not to be always right) using zero as argument should cause session to timeout immediately.
但是根据我的教科书(已经有几年的历史了 - 所以我希望它并不总是正确的)使用零作为参数应该会导致会话立即超时。
This code
这段代码
public class Test extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("foo", 42);
session.setMaxInactiveInterval(0);
out.println(session.getAttribute("foo"));//problem here
}
}
used on Glassfish 4.0 seems to confirm theory from textbook instead of newer official documentation because it returns HTTP Status 500 - Internal Server Errorwith error message
在 Glassfish 4.0 上使用似乎证实了教科书而不是较新的官方文档中的理论,因为它返回HTTP 状态 500 -带有错误消息的内部服务器错误
java.lang.IllegalStateException: getAttribute: Session already invalidated
java.lang.IllegalStateException: getAttribute: Session already invalidated
What is going on here? Is this Glassfish 4.0 bug or documentation is wrong? Or maybe there is third option?
这里发生了什么?这是 Glassfish 4.0 错误还是文档错误?或者也许有第三种选择?
PS. This code works as it should with negative values (session is not invalidated) and I am using -1
instead of 0
in my code. I am just interested what is wrong with 0
.
附注。这段代码在负值(会话未失效)的情况下正常工作,我在我的代码中使用-1
而不是0
。我只是对什么问题感兴趣0
。
采纳答案by Sotirios Delimanolis
The Servlet Specificationchapter on Session Timeouts states
Session Timeouts 上的Servlet 规范章节指出
By definition, if the time out period for a session is set to -1, the session will never expire.
根据定义,如果会话的超时期限设置为 -1,则会话将永不过期。
So GlasshFish seems to have that covered. I can't find any reference in the specification that says that the same should be true for a value of 0
with setMaxInactiveInterval()
. However it does say
所以 GlasshFish 似乎涵盖了这一点。我在规范中找不到任何参考资料,说明对于0
with的值也应该如此setMaxInactiveInterval()
。然而它确实说
The
session-config
defines the session parameters for this Web application. The sub-elementsession-timeout
defines the default session time out interval for all sessions created in this Web application. The specified time out must be expressed in a whole number of minutes. If the time out is 0 or less, the container ensures the default behavior of sessions is never to time out. If this element is not specified, the container must set its default time out period.
在
session-config
此Web应用程序定义会话参数。该子元素session-timeout
定义了在此 Web 应用程序中创建的所有会话的默认会话超时间隔。指定的超时必须以整数分钟数表示。如果超时为 0 或更少,则容器确保会话的默认行为永远不会超时。如果未指定此元素,则容器必须设置其默认超时期限。
回答by charitha
This is already time out and invalidate
这已经超时并且无效
session.setMaxInactiveInterval(0); // mean inactive immediately
So this is correct error message. (please refer head first book for further reference.)
所以这是正确的错误信息。(请参阅第一本书以供进一步参考。)
You are trying to access object value which is not exist. it's already destroyed
您正在尝试访问不存在的对象值。它已经被摧毁了