Show TOC

Procedure documentationRetrieving Cookies from the HTTP Request Locate this document in the navigation structure

 

Use the HTTP request object that is passed to the service method of your servlet to get all cookies that the client browser sent with the request.

Procedure

  1. Use the getCookies() method of the HttpServletRequest to retrieve an array of all cookies in the request.

  2. Loop down the array entries calling getName() on each Cookie object until you find the cookie you want to retrieve.

  3. Call the getValue() (or any of the other methods that javax.servlet.http.Cookie class defines to retrieve other parameters) on that object to get the value of this particular cookie.

    Example Example

    1. Cookie[] cookies = request.getCookies();
      String cookieName = SAPCookie;
      String defaultValue = 1;
      for(int i=0; i<cookies.length; i++) }
            Cookie cookie=cookies[i];
            if (cookieName.equals(cookie.getName()))
              return(cookie.getValue());
          }
          return(defaultValue);
        }
      
    End of the code.

Result

You have retrieved cookies that the client browser sent with the request.