Disable chunking per request

风格不统一 提交于 2019-12-08 02:58:17

问题


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 the ServletResponse.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 the Content-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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!