【发布时间】:2013-11-04 06:03:36
【问题描述】:
以下是我的代码。
在这, 会话超时立即,因为我们说, “0 秒不活动后超时”。
因此 session.getAttribute("foo") 必须给出异常。但是代码可以正常工作,就像会话根本没有超时一样。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute(“foo”, “42”);
session.setMaxInactiveInterval(0);
String foo = (String) session.getAttribute(“foo”);
if (session.isNew()) {
out.println(“This is a new session.”);
} else {
out.println(“Welcome back!”);
}
out.println(“Foo: “ + foo);
}
当我使用 getMaxInactiveInterval() 时,问题也类似。
当我第一次明确调用 setMaxInactiveInterval() 时,它会给出正确的结果。但是,如果没有以编程方式设置,getMaxInactiveInterval 不会显示默认会话超时,即 30 分钟。相反,它总是给出 60 秒 作为结果。
我的服务器是 Apache Tomcat 7。 感谢您的帮助
【问题讨论】:
-
在会话中设置一些东西并在不处理响应的情况下获取它有什么意义???
-
HeadFirst JSP 和 Servlet Book 中解释了代码以显示 Session 方法的语义。因此,请原谅我缺乏逻辑,如果有的话,从应用程序的角度来看,先生。感谢您的回复。