Setting headers on the HttpServletResponse after writing the response body

I realized that it is impossible to add more headers to the HttpServletResponse after starting to write the response body to the output buffer, which, I think, in some random universe may make sense, although everything is still stored in memory.

Now the question is: is there any trick to get around this anyway? Clearly, since the resetBuffer() function is available, which allows you to clear the content body without clearing the headers, there must be some way for the HttpServletResponse object to return to a state in which it is possible to write more headers. Is there, for example, a way to read the content body, clear it with resetBuffer() , set more headers and then restore the content body?

Beyond : The reason I would like to do this is to add a header as the very last step in my servlet, which tells me how long the server is busy processing the request.

+4
source share
1 answer

Your only choice is to buffer the response body yourself; When the body is finished, you then add the title, then record the body.

The funny thing is that HTTP / 1.1 has a way to send a header after the response body using an encoded trailer, but no one implements this, so don't worry.

+3
source

Source: https://habr.com/ru/post/1448031/


All Articles