Java 怎么判断httpservletrequest是否有cookie

2019年8月15日22:56:52Java 怎么判断httpservletrequest是否有cookie已关闭评论 251 486字阅读1分37秒

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 boolean hasCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        boolean foundCookie = false;
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie1 = cookies[i];
            if (cookie1.getName().equals(name)) {
                foundCookie = true;
            }
        }
        return foundCookie;
    }
}

 
编程十万个怎么办