Posted on 2005-12-06 10:15
☆蓝色梦想☆ 阅读(426)
评论(0) 编辑 收藏 所属分类:
J2EE
<%@page contentType="text/html; charset=GBK"%><%
//判断客户端是否打开Cook
if (request.getParameter("flag") == null)
{
// the first request
Cookie cookie = new Cookie("cookieSetting","on");
response.addCookie(cookie);
String nextUrl = request.getRequestURI() +"?flag=1";
// force the browser to refresh
out.println(nextUrl);
//out.println("<META HTTP-EQUIV=Refresh CONTENT = 0; URL = " + nextUrl +" > ");
}
else
{
// the second request
Cookie[] cookies = request.getCookies();
boolean cookieFound = false;
if (cookies != null) {
int length = cookies.length;
for (int i = 0; i < length; i++) {
Cookie cookie = cookies[i];
if
(cookie.getName().equals("cookieSetting") &&
cookie.getValue().equals("on")) {
cookieFound = true;
break;
}
}
}
if (cookieFound) {
out.println("Cookie is on.");
}
else {
out.println("Cookie is off.");
}
}
%>