Is it incorrect to add a cookie to the response, after you already wrote to the output buffer?
I have a bug where I add a cookie to the HttpServletResponse
, but the cookie doesn't get set at all.
It is added in a servlet filter, after the jsp page has already been rendered. Is the reason it isn't being added because it is being added after some content has already been written to the output stream?
Cookies get sent in the HTTP headers, so you cannot add them after the response has been committed. You can test whether the response has been committed using response.isCommitted(). A response is not usually committed as soon as you write the first byte to the response, but as soon as you have flushed the response or filled the response buffer.
Yes. Cookies need to be set before content's written - they're sent to the client as headers.
Since a cookie is part of the header, it has to be set into the response before the response is written to the output.
Even though there is a failure, it will not throw an exception.
精彩评论