无法在 JSP 中设置标头。响应已提交
WebSphere 记录警告消息“SRTServletRes W 警告:无法设置标头。对于一个 JSP 请求,响应已提交”。我稍后在代码中需要响应标头。我做了一些研究并了解到 Servlet 正在尝试将更多数据发送到输出流,但该流已经被提交。我不明白为什么这种情况只发生在这个特定的 JSP 上,因为这个 Servlet 代码对于其他 JSP 工作得很好。此页面未重定向,我收到的响应没有响应标头。
WebSphere logs the warning message “SRTServletRes W WARNING: Cannot set header. Response already committed” for one JSP request. I need the respone headers later in my code. I did some research and understood that Servlet is trying to send more data to the output stream, but the stream is already been committed. I did not understand why this is happening to only this particular JSP, as this Servlet code works fine for other JSPs. This page is not redirected and I get the response back with no response headers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当响应被提交时,这意味着至少标头已经发送到客户端。当响应已经提交时,您无法设置/更改标头,因为为时已晚。
只要满足以下一个或多个条件,就会提交响应:
HttpServletResponse#sendRedirect()
。flush()
。2K 缓冲区限制可在应用程序服务器的配置中进行配置。
您需要重新排列代码逻辑,以便它仅在提交响应之前设置标头。您不应该永远在 JSP 内部/中间使用 scriptlet 设置/更改响应标头。您应该只在继续链之前在
Filter
中执行此操作,或者在分派请求之前在页面控制器Servlet
中执行此操作。还要注意 JSP 包含文件不会调用它们。When a response is committed, it means that at least the headers are already been sent to the client side. You cannot set/change headers when the response is already committed, because it's too late.
A response will be committed whenever one or more of the following conditions is met:
HttpServletResponse#sendRedirect()
has been called.flush()
was been invoked on the response output stream, either by Servlet or JSP.The 2K buffer limit is configureable in appserver's configuration.
You need to rearrange the code logic so that it only sets the headers before the response is committed. You should never set/change the response headers using scriptlets inside/halfway a JSP. You should only do that in a
Filter
before continuing the chain, or in a page controllerServlet
before dispatching the request. Also take care that neither of them are been called by a JSP include file.