[Logo]
Forums Register Login
setContentLength
I'm puzzled with this one. The API says that the ServletResponse.setContentLength(int len) method sets the HTTP Content-Length header.
How can the Servlet container or Servler programmer know in advance how long the content will be and set it first in the header before the content is even produced?
Any idea?
Francois
No comments with respect to HTTP response...but i have used this to write ms-word or pdf files to the client.
Usually, before even the header is given back to the browser, servlets buffer the response, so you can set the content length on the fly.
As far as figuring out the length of content, you usually don't have to unless it's an unusual format (dynamically-created gif, pdf, serialized object, etc). In this case, just set the content-length equal to the byte-length of the file or object that you want to send back.

Originally posted by Francois Merle:
I'm puzzled with this one. The API says that the ServletResponse.setContentLength(int len) method sets the HTTP Content-Length header.
How can the Servlet container or Servler programmer know in advance how long the content will be and set it first in the header before the content is even produced?
Any idea?
Francois


You have to compute it. The content-length isn't mandatory in most cases, but there's one infamous exception - if you write a PDF, Netscape will choke on it if there's no content-length. I had to write the PDF to a temp file, get the length of the temp file, set the content-length header, then spool the temp file out to the client to make this work.
We noticed he had no friends. So we gave him this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 1320 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 21, 2018 04:01:56.