Searches for a cookie within a HttpServletRequest. - Java Servlet JSP

Java examples for Servlet JSP:Cookie

Description

Searches for a cookie within a HttpServletRequest.

Demo Code


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

public class Main{
    /**//from www .java  2 s  . c o  m
     * Searches for a cookie within a HttpServletRequest.
     * 
     * @param name the name of the cookie.
     * @param request the HttpServletRequest to find the cookie on.
     * @return the cookie
     */
    public static Cookie getCookieForName(String name,
            HttpServletRequest request) {
        if (request.getCookies() == null) {
            return null;
        }
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equalsIgnoreCase(name)) {
                return cookie;
            }
        }
        return null;
    }
}

Related Tutorials