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

查看:471
本文介绍了无法在JSP中设置标头。响应已经提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebSphere记录警告消息SRTServletRes W警告:无法设置标头。对于一个JSP请求已经提交的响应。我稍后需要在代码中使用respone标头。我做了一些研究,并了解到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()

  • 已经通过Servlet或JSP将超过2K写入响应输出。

  • 已写入超过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缓冲区限制可以在appserver的配置中配置。

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

您需要重新排列代码逻辑,以便在提交响应之前设置标头。您应该永远使用JSP内部的 scriptlet 来设置/更改响应标头。您应该在继续链之前在过滤器中执行此操作,或者在分派请求之前在页面控制器 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天全站免登陆