java中cookie的元素无论如何也删不掉!跪求解答!
HttpServletResponseresponse=(HttpServletResponse)ActionContext.getContext().get(Strut...
HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
Cookie ck[]=request.getCookies();
if(ck!=null)
{
for(int i=0;i<ck.length;i++)
{
Cookie cc=ck[i];
if("name".equals(cc.getName()) || "pwd".equals(cc.getName()))
{
cc.setMaxAge(0);
cc.setPath("/");
response.addCookie(cc);
}
}
}
调试过程中看到cookie中元素一个也没变。。。束手无策啊!! 展开
HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
Cookie ck[]=request.getCookies();
if(ck!=null)
{
for(int i=0;i<ck.length;i++)
{
Cookie cc=ck[i];
if("name".equals(cc.getName()) || "pwd".equals(cc.getName()))
{
cc.setMaxAge(0);
cc.setPath("/");
response.addCookie(cc);
}
}
}
调试过程中看到cookie中元素一个也没变。。。束手无策啊!! 展开
1个回答
展开全部
你可以这样删除Cookie
public boolean deleteCookie(String cookieName) {
if (cookieName != null) {
Cookie cookie = getCookie(cookieName);//getCookie()方法在下面
if (cookie != null) {
cookie.setMaxAge(0);// 如果0,就说明立即删除
cookie.setPath("/");// 不要漏掉
response.addCookie(cookie);
return true;
}
}
return false;
}
public Cookie getCookie(String cookieName) {
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
try {
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.getName().equals(cookieName)) {
return cookie;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
希望对你有所帮助!
public boolean deleteCookie(String cookieName) {
if (cookieName != null) {
Cookie cookie = getCookie(cookieName);//getCookie()方法在下面
if (cookie != null) {
cookie.setMaxAge(0);// 如果0,就说明立即删除
cookie.setPath("/");// 不要漏掉
response.addCookie(cookie);
return true;
}
}
return false;
}
public Cookie getCookie(String cookieName) {
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
try {
if (cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.getName().equals(cookieName)) {
return cookie;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
希望对你有所帮助!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询