2

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Possible duplicate of [How do disable Transfer-Encoding in Tomcat 6](http://stackoverflow.com/questions/6299432/how-do-disable-transfer-encoding-in-tomcat-6) – Vadzim Sep 13 '16 at 11:56

1 Answers1

2

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.

...

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • that makes sense, but it doesn't work on glassfish. the server still chunks even when the content length is set. – Jeffrey Blattman Sep 29 '11 at 18:52
  • What Glassfish version? Are you certain that the header is set *before* any content is written? Do you see the header in the client side? Are you sure that you aren't setting the header in the resource which is been called by `` or `RequestDispatcher#include()` (that would namely be totally ignored, see also [its javadoc](http://download.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#include%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29)). – BalusC Sep 29 '11 at 18:55
  • GF 3.1; the header does not show, i still see "chunked"; no i'm not writing any content before setting the header – Jeffrey Blattman Sep 29 '11 at 20:32
  • I'd bet that there's somewhere a bogus `HttpServletResponseWrapper` implementation in the request-response chain which is preventing that. Put a breakpoint on `setContentLength()` and track from it on. – BalusC Sep 29 '11 at 20:33