Java 怎么清除servlet cookie

2019年8月15日22:58:47 评论 35

1.简介

Java 怎么清除servlet cookie?——请看下文。

2.代码

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

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

public class Main{
    public static boolean cleanCookie(HttpServletResponse response,
            String[] keys) {
        return UtilCookie.setCookie(response, 0, keys,
                new String[keys.length]);
    }
    public static boolean setCookie(HttpServletResponse response,
            int maxAge, String[] keys, String[] values) {
        if (keys != null && values != null && keys.length == values.length) {
            maxAge = maxAge < -1 ? -1 : maxAge;
            for (int i = 0; i < keys.length; i++) {
                Cookie myCookie = new Cookie(keys[i], values[i]);
                myCookie.setPath("/");
                myCookie.setMaxAge(maxAge);
                response.addCookie(myCookie);
            }
            return true;
        }
        return false;
    }
}

 

3.总结

示例代码演示了 Java 怎么清除servlet cookie,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: