除了关闭浏览器(我没有删除 cookie)之外,我的 servlet 正在按预期工作,会话丢失。如何无限期地保存会话,直到我将其无效或删除我的 cookie?
@WebServlet(name="ServletOne", urlPatterns={"/", "/ServletOne"})
public class ServletOne extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String newValue = request.getParameter("newValue");
if (session.isNew()) {
session = request.getSession(true);
session.setAttribute("myAttribute", "value");
}
if (newValue != null)
session.setAttribute("myAttribute", newValue);
RequestDispatcher rd = request.getRequestDispatcher("test.jsp");
rd.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
我的 JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
val == <c:out value="${myAttribute}"></c:out><br>
<form action="ServletOne" method="POST">
<input type="text" name="newValue" />
<input type="submit" />
</form>
</body>
</html>
如果我关闭浏览器并重新打开它,myAttribute
总是设置为默认的“值”。