How to Set the Content Length
If you already know the size of your response body before you start writing to the OutputStream (e.g. you have the response as a byte array or file), then you can help container and client by calling the setContentLength method:
request.setContentLength(1024); // set length in bytes
The method sets the Content-Length header value in the response and allows various optimizations in both server and client. Note that the server will automatically close the OutputStream as soon as you wrote the specified amount of bytes.
Setting the content length is usually no good idea when using a PrintWriter for output, as you can't know the content length in bytes without encoding the characters first (and if you already have an encoded result, just use an OutputStream).

