求救,我已经烦了两天了。JSP删除不了cookie?
删除cookie的代码如下:
Cookie[] cookies=request.getCookies();
//cookies不为空,则清除
if(cookies!=null)
{
for(int i=0;i<cookies.length;i++)
{
String tempuid_1=cookies[i].getName();
//查找用户名
if(tempuid_1.equals("uid"))
{
cookies[i].setValue(null);
response.addCookie(cookies[i]);
System.out.println("sdfsf");
}
//查找密码
if(tempuid_1.equals("pwd"))
{
cookies[i].setValue(null);
response.addCookie(cookies[i]);
System.out.println("sdfsf");
}
}
}
读cookie的代码如下:
if (request.getCookies() != null) {
//get coolie array from http request
Cookie[] cookie = request.getCookies();
for (int i = 0; i < cookie.length; i++) {
Cookie myCookie = cookie[i];
//get your named cookie from array
if (myCookie.getName().equals("uid")) {
//get your cookie's value
uid = myCookie.getValue();
}
if (myCookie.getName().equals("pwd")) {
//get your cookie's value
pwd = myCookie.getValue();
}
}
}