在写入响应主体后在HttpServletResponse上设置标头 [英] Setting headers on HttpServletResponse after writing response body

查看:1087
本文介绍了在写入响应主体后在HttpServletResponse上设置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始将响应体写入输出缓冲区之后,我想出了不可能在 HttpServletResponse 中添加更多标头的困难方法,其中,I猜测一些随机的宇宙可能有意义,即使所有东西仍然只是缓冲在内存中。

I figured out the hard way that it isn't possible to add more headers to an HttpServletResponse after beginning to write the response body to the output buffer, which, I guess in some random universe could make sense even though everything is still just buffered in memory.

现在的问题是:是否有一些技巧可以以某种方式绕过这个?显然,由于函数 resetBuffer()可用,它允许在不清除标题的情况下清除内容正文,因此 HttpServletResponse必须有一些方法。 返回可以写入更多标题的状态的对象。例如,是否有一种方法来读取内容正文,使用 resetBuffer()清除它,设置更多标题,然后恢复内容正文?

Now the question is: Is there some trick to circumvent this somehow? Clearly, since the function resetBuffer() is available, which allows clearing of the content body without clearing the headers, there must be some way for the HttpServletResponse object to return to the state where writing more headers was possible. Is there, for example, a way to read the content body, clearing it with resetBuffer(), setting more headers, and then restoring the content body?

除了
我想这样做的原因是我可以添加一个标题作为我的servlet的最后一步,它告诉我服务器忙于处理请求多长时间。

推荐答案

你唯一的选择是缓冲响应身体自己;当body完成后,你然后添加标题,然后写入正文。

Your only choice is to buffer the response body yourself; when body is finished, you then add the header, then write the body.

有趣的是,HTTP / 1.1有一种方法可以在响应体后发送标题,通过在分块编码中使用预告片,但没有人实现,所以不要打扰。

The funny thing is, HTTP/1.1 has a way to ship a header after the response body, by using trailer in chunked encoding, but nobody implements that, so don't bother.

这篇关于在写入响应主体后在HttpServletResponse上设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆