“响应已提交"到底是什么意思?意思是?那么如何处理异常呢? [英] What exactly does "Response already committed" mean? How to handle exceptions then?

查看:265
本文介绍了“响应已提交"到底是什么意思?意思是?那么如何处理异常呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在getter和setter中编写业务逻辑是非常糟糕的编程习惯,但是如果响应已经提交,有什么方法可以处理异常?

I know writing business logic in getters and setters is a very bad programming practice, but is there any way to handle exceptions if the response is already committed?

已提交响应"和报头已发送到客户端"的确切含义是什么?

What exactly is the meaning of "Response already committed" and "Headers are already sent to the client"?

推荐答案

如果响应已经提交,则没有很好的方法来处理异常. HTTP响应基本上存在标头和主体.标头基本上指示客户端(网络浏览器)应如何准确地处理响应,例如内容类型,内容长度,字符编码,主体编码,缓存指令等.

There's no nice way to handle exceptions if the response is already committed. The HTTP response exist basically of a header and a body. The headers basically instruct the client (the webbrowser) how exactly it should deal with the response, e.g. the content type, the content length, the character encoding, the body encoding, the cache instructions, etcetera.

您可以在Webbrowser的开发人员工具集的HTTP流量监视器中看到标头.在Chrome/IE9 +/Firefox23 +中按F12,然后选中网络"标签.以下屏幕显示是我的Chrome在您当前问题上显示的内容:

You can see the headers in the HTTP traffic monitor of the webbrowser's developer toolset. Press F12 in Chrome/IE9+/Firefox23+ and check the "Network" tab. The below screenshow is what my Chrome shows on your current question:

(注意:响应"标签显示响应正文)

响应主体是实际内容,通常具有一堆HTML代码的形式.服务器通常具有一个固定大小的缓冲区,用于将响应写入其中.缓冲区大小取决于服务器的制造/版本和配置,通常为2KB〜10KB.如果该缓冲区溢出,则将其刷新到连接的另一端,即客户端.这是响应的提交.客户已经获得了响应的第一部分,通常已经代表了整个头文件,甚至可能是正文的一部分.

The response body is the actual content, usually in flavor of a bunch of HTML code. The server has usually a fixed size buffer to write the response to. The buffer size depends on server make/version and configuration and is usually 2KB~10KB. If this buffer overflows, then it will be flushed to the other end of the connection, the client. This is the commit of a response. The client has already obtained the first part of the response, usually already representing the whole bunch of headers and maybe a part of the body.

提交响应是没有回报的点.服务器无法取回已发送的字节.更改响应标头为时已晚(例如,重定向基本上是由Location标头指示的,其中包含新URL),更不用说响应主体了.最好的办法是将错误信息附加到已编写的响应正文中.但这可能会导致看起来有些奇怪的HTML,因为目前尚不知道需要关闭哪些HTML标签.浏览器可能无法正确显示它.

The commit of a response is a point of no return. The server cannot take the already sent bytes back. It's too late to change the response headers (for example, a redirect is basically instructed by a Location header with therein the new URL), let alone the response body. Best what you can do is to append the error information to the already written response body. But this may end up in some weird looking HTML as it's not known which HTML tags needs to be closed at that point. The browser may fail to present it in a proper manner.

除了避免在getter中使用业务逻辑,以便在呈现响应时不会引发异常外,避免已提交的响应的另一种方法是将响应缓冲区的大小配置为与webapp可以服务的最大页面一样大.该如何做取决于服务器的版本.例如,在Tomcat中,您可以将其配置为 bufferSize属性<Connector>元素.请注意,如果您自己的代码(隐式)在响应输出流上调用flush(),则这将不会阻止刷新.

Apart from avoiding business logic in getters so that the exceptions are not thrown while rendering the response, another way to avoid an already committed response is to configure the response buffer size to be as large as the largest page which your webapp can serve. How to do that depends on the server make/version. In Tomcat for example, you can configure it as bufferSize attribute of the <Connector> element. Note that this won't prevent from flushing if your own code is (implicitly) calling flush() on the response output stream.

这篇关于“响应已提交"到底是什么意思?意思是?那么如何处理异常呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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