get cookie from HttpServletRequest - Java Servlet JSP

Java examples for Servlet JSP:Cookie

Description

get cookie from HttpServletRequest

Demo Code


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

public class Main{
    public static Cookie get(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie;
                }/*from   ww  w. j a va  2 s  . c o  m*/
            }
        }
        return null;
    }
}

Related Tutorials