这是我的 servlet,它创建会话并将 accessCount 存储到当前会话
@WebServlet("/ShowSession.do")
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession();
String header;
Integer accessCount = (Integer)request.getAttribute("accessCount");
if(accessCount == null){
accessCount = new Integer(0);
header = "Welcome New Comer";
}else{
header = "Welcome Back!";
accessCount = new Integer(accessCount.intValue() + 1 );
}
//Integer is immutable data structure. so we cannot
//modify the old one in-place,Instead, you have to
//allocate a new one and redo setAttribute
session.setAttribute("accessCount", accessCount);
session.setAttribute("heading", header);
RequestDispatcher view = getServletContext().getRequestDispatcher("/showsession.jsp");
view.forward(request, response);
}
}
这是打印出该会话内容的视图
<body>
<%
HttpSession clientSession = request.getSession();
String heading = (String) clientSession.getAttribute("heading");
Integer accessCount = (Integer) clientSession
.getAttribute("accessCount");
%>
<h1>
<center>
<b><%=heading%></b>
</center>
</h1>
<table>
<tr>
<th>Info type</th>
<th>Value</th>
<tr>
<tr>
<td>ID</td>
<td><%=clientSession.getId()%></td>
</tr>
<tr>
<td>Creation Time</td>
<td><%=new Date(clientSession.getCreationTime())%></td>
</tr>
<tr>
<td>Time of last Access</td>
<td><%=new Date(clientSession.getLastAccessedTime())%></td>
</tr>
<tr>
<td>Number OF Access</td>
<td><%=accessCount%></td>
</tr>
</table>
</body>
问题是,即使我已经访问了很多次,它仍然返回我一个 accessCount == 0,
我在 localhost:8080/someFolderName/ShowSession.do 访问