JAvaセグメント出力(chunked)

2074 ワード

JAvaセグメント出力(chunked)
//出力クラス容量のタイプを設定し、以下の設定に従って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(「こんにちは!
」);
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)
***/
@Source:
@Author:zlong