无法在 JSP 中设置标头.响应已经提交 [英] Cannot set header in JSP. Response already committed

查看:55
本文介绍了无法在 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.

推荐答案

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

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() 已被调用.
  • 超过 2K 的数据已经被 Servlet 或 JSP 写入响应输出.
  • 已写入超过 0K 但小于 2K 并且 flush() 被 Servlet 或 JSP 对响应输出流调用.
  • 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.

2K 缓冲区限制可在应用程序服务器的配置中进行配置.

The 2K buffer limit is configureable in appserver's configuration.

您需要重新排列代码逻辑,使其仅在提交响应之前设置标题.您应该永远在 JSP 内部/中途使用 scriptlets 设置/更改响应标头.您应该只在继续链之前在 Filter 中执行此操作,或者在分派请求之前在页面控制器 Servlet 中执行此操作.还要注意它们都没有被 JSP 包含文件调用.

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.

这篇关于无法在 JSP 中设置标头.响应已经提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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