public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        // Return null if there are no cookies or the name is invalid.
        if (cookies == null || name == null || name.isEmpty()) {
            return null;
        }
        // Otherwise, we  do a linear scan for the cookie.
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            // If the current cookie name matches the one we're looking for, we've
            // found a matching cookie.
            if (cookies[i].getName().equals(name)) {
                cookie = cookies[i];
                // The best matching cookie will be the one that has the correct
                // domain name. If we've found the cookie with the correct domain name,
                // return it. Otherwise, we'll keep looking for a better match.
                if (request.getServerName().equals(cookie.getDomain())) {
                    break;
                }
            }
        }
        return cookie;
    }
Returns the specified cookie, or {@code null} if the cookie
does not exist. Note: because of the way that cookies are implemented
it's possible for multiple cookies with the same name to exist (but with
different domain values). This method will return the first cookie that
has a name match.

@param request the servlet request.
@param name the name of the cookie.
@return the Cookie object if it exists, otherwise {@code null}.
xmppserver/src/main/java/org/jivesoftware/util/CookieUtils.java