has Cookie in HttpServletRequest - Java Servlet JSP

Java examples for Servlet JSP:Cookie

Description

has Cookie in HttpServletRequest

Demo Code


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;/*  w  w w  . j  av a 2  s.com*/
            }
        }
        return foundCookie;
    }
}

Related Tutorials