Java 怎么从httpservletrequest中删除cookie

2019年8月15日22:57:06Java 怎么从httpservletrequest中删除cookie已关闭评论 181 595字阅读1分59秒

1.简介

Java 怎么从httpservletrequest中删除cookie?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Main{
    public static void removeCookies(HttpServletRequest request,
            HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            removeCookie(response, cookie.getName());
        }
    }
    public static void removeCookie(HttpServletResponse response,
            String name) {
        Cookie cookie = new Cookie(name, "");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

 
编程十万个怎么办