get Cookie value from HttpServletRequest - Java Servlet JSP

Java examples for Servlet JSP:Cookie

Description

get Cookie value from HttpServletRequest

Demo Code


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

public class Main{
    public static String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie1 = cookies[i];
            if (cookie1.getName().equals(name)) {
                return cookie1.getValue();
            }/*from  w w w .j a v  a2  s  .co  m*/
        }
        return null;
    }
}

Related Tutorials