无法在 JSP 中设置标头。响应已提交

发布于 2024-08-17 03:04:36 字数 206 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-08-24 03:04:36

当响应被提交时,这意味着至少标头已经发送到客户端。当响应已经提交时,您无法设置/更改标头,因为为时已晚。

只要满足以下一个或多个条件,就会提交响应:

  • 已调用 HttpServletResponse#sendRedirect()
  • 超过 2K 的内容已通过 Servlet 或 JSP 写入响应输出。
  • 已写入多于 0K 但少于 2K 的内容,并且已通过 Servlet 或 JSP 在响应输出流上调用了 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.
  • More than 2K has already been written to the response output, either by Servlet or JSP.
  • More than 0K but less than 2K has been written and 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 controller Servlet before dispatching the request. Also take care that neither of them are been called by a JSP include file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文