Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setContentLength

 
Paul Medford
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manjunath Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No comments with respect to HTTP response...but i have used this to write ms-word or pdf files to the client.
 
jasonkosowan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Tim Holloway
Saloon Keeper
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic