|
Initially contacted cookies, and wrote a small program according to the example in the book, but why does n’t cookie-AccessTimes reset to 1 each time I reset resetCookie.jsp? (Now accumulates 1 for each refresh)
The code is very simple, I will post it here in full.
resetCookie.jsp:
<% @ page language = "java"%>
<% @ page pageEncoding = "GB2312"%>
<html>
<head>
<title> jsp session tracking </ 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 demo </ 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> |
|