posts - 1,  comments - 3,  trackbacks - 0
//设置输出类容的类型,按照下面设置后flush()才会起作用
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
for(int index = 0 ;index < 5 ;index++){
writer.write("你好!<br />");
writer.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
writer.close();

/***

不设置ContentType直接使用flush()不会有作用。

如果要flush()起作用,必须先向缓冲区填充一定的内容(测试为1024个字符)。
网上看到的原因是容器认为缓冲区的内容过少发送的效率不高。


设置了ContentType为text/html后,flush()就会立即起作用。
可能是因为动态生成Html的时候加载数据比较耗时,可以先返回后让浏览器解析样式。(个人猜想)

上面的内容没有在任何的官方文档中看到。唯一在官方文档中看到和内容大小有关系的是关于Buffer大小的设置:

setBufferSize

public void setBufferSize(int size)
Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize.

A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

This method must be called before any response body content is written; if content has been written or the response object has been committed, this method throws an IllegalStateException.

 

Parameters:
size - the preferred buffer size
Throws:
java.lang.IllegalStateException - if this method is called after content has been written
See Also:
getBufferSize()flushBuffer()isCommitted()reset()

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletResponse.html#setBufferSize(int)


***/
posted @ 2016-10-09 18:22 jidebingfeng 阅读(1266) | 评论 (0)编辑 收藏
<2016年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿

随笔分类

随笔档案

文章档案

搜索

  •  

最新评论