Is there any way to read cookies from the response object in Java?

后端 未结 4 1887
梦谈多话
梦谈多话 2020-12-29 05:55

It doesn\'t seem that HttpServletResponse exposes any methods to do this.

Right now, I\'m adding a bunch of logging code to a crufty and ill-understood servlet, in

4条回答
  •  长情又很酷
    2020-12-29 06:22

    Cookies are sent to the client in the "Set-Cookie" response header. Try this:

    private static void logResponseHeaders(HttpServletResponse httpServletResponse) {
    
        Collection headerNames = httpServletResponse.getHeaderNames();
    
        for (String headerName : headerNames) {
            if (headerName.equals("Set-Cookie")) {
                log.info("Response header name={}, header value={}", headerName, httpServletResponse.getHeader(headerName));
            }
        }
    }
    

提交回复
热议问题