servlet get Cookie - Java javax.servlet.http

Java examples for javax.servlet.http:Cookie

Description

servlet get Cookie

Demo Code


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

public class Main{
    private final static Logger logger = Logger
            .getLogger(AuthenticationCookieUtils.class);
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookie = null;//from w  ww.j  av  a 2s  .  co  m
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals(name)) {
                    cookie = c;
                    break;
                }
            }
        }

        if (cookie == null) {
            logger.debug("Cookie " + name + " not found");
        } else {
            logger.debug("Cookie " + name + " found");
        }

        return cookie;
    }
}

Related Tutorials