|
初步接触cookie,按照书上的例子写了一个小程序,但是为什么每次刷新resetCookie.jsp,cookie-AccessTimes不重置为1?(现在每次刷新都累加1)
代码非常简单,我就全文贴在这里了。
resetCookie.jsp:
<%@ page language="java" %>
<%@ page pageEncoding="GB2312" %>
<html>
<head>
<title>jsp 会话跟踪</title>
</head>
<body>
<%
response.addCookie(new Cookie("AccessTimes","1"));
%>
<jsp:forward page="useCookie.jsp" />
</body>
</html>
useCookie.jsp:
<%@ page pageEncoding="GB2312" language="java" %>
<html>
<head>
<title>cookie演示</title>
</head>
<body>
<%
Cookie[] cookies=request.getCookies();
String counter_S=null;
int counter;
if(cookies!=null)
{
for(int i=0;i<cookies.length;i++)
{
if(cookies[i].getName().equals("AccessTimes"))
counter_S=cookies[i].getValue();
}
try
{
counter=Integer.parseInt(counter_S)+1;
}
catch(Exception ex)
{
counter=1;
}
response.addCookie(new Cookie("AccessTimes",new Integer(counter).toString()));
out.println("You have visited the site for"+counter+"times!");
}
%>
</body>
</html>
|
|