Cookie的应用,高手帮帮忙,好久都没测出来哦,先谢了
<%@page import = "javax.servlet.http.Cookie, java.util.*"%><title>cookie应用 </title>
<%
String userName = "cookitest";
Cookie[] cookie = request.getCookies();
Cookie myCookie = null;
List list = Arrays.asList(cookie);
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
Cookie temp = (Cookie)iterator.next();
if(temp.getName().equals(userName + "access time" )) {
myCookie = temp;
break;
}
}
out.println("当前时间为:" + new Date() + "<br>");
if(myCookie != null) {
out.println("上次访问的时间:" + myCookie.getValue());
myCookie.setValue(new Date().toString());
} else {
myCookie = new Cookie(userName + " access time", new Date().toString);
}
response.addCookie(myCookie);
response.flushBuffer();
%>