Getting cookie in servlet

后端 未结 5 1818
情话喂你
情话喂你 2021-01-04 05:47

I\'m trying to get cookie in servlet using

Cookie[] cookie = request.getCookies();

but cookie is always null.

5条回答
  •  误落风尘
    2021-01-04 06:08

    According to docs getCookies() Returns an array containing all of the Cookie objects the client sent with this request. This method returns null if no cookies were sent.

    Did you add the cookie correctly? If yes, you should be able to iterate through the list of cookies returned with

    Cookie[] cookies = request.getCookies();
    
    for (int i = 0; i < cookies.length; i++) {
      String name = cookies[i].getName();
      String value = cookies[i].getValue();
    }
    

    If no...

    Cookies are added with the addCookie(Cookie) method in the response object!

提交回复
热议问题