HttpServletResponse输入输出流是否需要flush close
2017-10-26 18:06
405 查看
1,是否需要
close the
servlet outputstream?
Normally you should not close the stream. The servlet container will automatically close the stream after the servlet is finished running as part of the servlet request life-cycle.For instance, if you closed the stream it would not be available if you implemented a Filter.
Having said all that, if you do close it nothing bad will happen as long as you don't try to use it again.
EDIT: another
filter link
EDIT2: adrian.tarau is correct in that if you want to alter the response after the servlet has done its thing you should create a wrapper extending HttpServletResponseWrapper and buffer the output. This is to keep the output from going directly to the client
but also allows you to protect if the servlet closes the stream, as per this excerpt (emphasis mine):
A filter that modifies a response must usually capture the response before
it is returned to the client. The way to do this is to pass the servlet that generates the response a stand-in stream. The stand-in stream prevents the servlet from closing the original response stream when it completes and allows the filter to modify the
servlet's response.
Article
One can infer from that official Sun article that closing the outputstream from a servlet is something that is a normal occurrence, but is not mandatory.
2,是否需要 flush
the servlet outputstream?
You don't need to. The servletcontainer will flush and close it for you. The close by the way already implicitly calls flush.See also chapter 5.6 of Servlet
3.1 specification:
5.6 Closure of Response Object
When a response is closed, the container must immediately flush all remaining content in the response buffer to the client. The following events indicate that the servlet has satisfied the request and that the response object is to be closed:The termination of the
servicemethod
of the servlet.
The amount of content specified in the
setContentLengthor
setContentLengthLongmethod
of the response has been greater than zero and has been written to the response.
The
sendErrormethod
is called.
The
sendRedirectmethod
is called.
The
completemethod
on
AsyncContextis
called.
Calling flush while still running the servlet's service is usually only beneficial when you have multiple writers on the same stream and you want to switch of the writer (e.g. file with mixed binary/character data), or when you want to keep the stream pointer
open for an uncertain time (e.g. a logfile).
转载自:
https://stackoverflow.com/questions/1159168/should-one-call-close-on-httpservletresponse-getoutputstream-getwriter https://stackoverflow.com/questions/5043657/do-i-need-to-flush-the-servlet-outputstream
相关文章推荐
- 【JavaWeb-6】HttpServletResponse的字符字节输出流、编码、文件下载、Captcha图片与HttpServletRequest获取request数据表单非表单数据、重定向与转发
- ServletResponse(或ServletRequest)中输入输出流的问题
- 验证调用HttpServletResponse.getWriter().close()方法是否真的会关闭http连接
- HttpServletResponse中获取PrintWriter需要注意的一个小问题
- HttpServletResponse中获取PrintWriter需要注意的一个小问题
- HttpServletResponse的字符字节输出流、编码、文件下载、Captcha图片与HttpServletRequest获取request数据表单非表单数据、重定向与转发
- HttpServletResponse getOutputStream 点击超链接更换图片(需要js函数的配合)
- 不二极端编程之JavaWeb详解(HttpServletRequest,HttpServletResponse)
- HttpServletResponse
- HttpServletResponse
- Filter中使用 HttpServletResponseWrapper修改response内容
- HttpServletResponse addHeader() 与 setHeader() 区别
- HttpServletResponse的应用
- HttpServletResponse的应用
- HttpServletResponse-生成验证码
- HttpServletResponse和HttpServletRequest详解
- Servlet中HttpServletResponse的应用
- Servlet中HttpServletResponse的应用
- HttpServletRequestAnd HttpServletResponse
- HttpServletResponse addHeader() 与 setHeader() 区别