问题
I have a web service backed by a Java Servlet. The service is used by an older version of Flash. We discovered through some pain that in this version of Flash, URLLoader
won't work with chunked responses. Any chunked response is never received from the server.
I am using Glassfish to host the Servlet. I know how to disable chunking for the entire server, but that seems like a bad idea (is it?).
Is there a standard way to disable chunking per request? I tried calling ServletResponse.setBufferSize(SOME_LARGE_VALUE)
but surprising this did not affect the server's decision to use chunking.
回答1:
From the javadoc of HttpServlet#doGet()
:
...
Where possible, set the
Content-Length
header (with theServletResponse.setContentLength(int)
method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.When using HTTP 1.1 chunked encoding (which means that the response has a
Transfer-Encoding
header), do not set theContent-Length
header....
So, if you set the response content length beforehand, then it won't be sent in chunked encoding.
response.setContentLength(contentLength);
// ...
Update: You also need to make sure that the servlet isn't in turn been called by <jsp:include>
or RequestDispatcher#include()
. See also its javadoc:
...
The
ServletResponse
object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored....
来源:https://stackoverflow.com/questions/7591132/disable-chunking-per-request