File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes when to use setContentLength() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "when to use setContentLength()" Watch "when to use setContentLength()" New topic
Author

when to use setContentLength()

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I have seen this pattern in several places, but this snippet is from Sun's j2ee tutorial.

My question is: When we need to use setContentLength()?

I would think that the OuputStream provided by the servlet container would always increment the Content-Length header field when adding bytes to the response, but it appears not.




Mike Gershman
SCJP 1.4, SCWCD in process
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12268
    
    1
I would think that the OuputStream provided by the servlet container would always increment the Content-Length header field when adding bytes to the response, but it appears not.

Now how in the world could it do that? The headers are sent when the first output buffer is full (or flushed.) Typically before the response has been completely generated.
In my experience, some plug-ins - such as PDF viewers - do not work correctly if the content-length is not supplied.
Bill


Java Resources at www.wbrogden.com
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The headers are sent when the first output buffer is full (or flushed.) Typically before the response has been completely generated.


That explains why the Content-Length header field is not set by the servlet container.

However, I notice that most code examples do not set the Content-Length header field while a few do. How do I know when this is necessary?
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 471
You shouldn't worry about setting this field. If chunked encoding used, then content length shouldn't be set, in this case you can also set some headers in the end of stream. Some servlet containers are capable buffer your servlet out, so tey set content length when you close stream.


Get power of your iPod with MediaChest | Minimal J2EE container is here | Light weight full J2EE stack | My blog | Co-author of "Windows programming in Turbo Pascal"
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12268
    
    1
You shouldn't worry about setting this field. If chunked encoding used, then content length shouldn't be set, in this case you can also set some headers in the end of stream. Some servlet containers are capable buffer your servlet out, so tey set content length when you close stream.

1. setting headers in the "end of stream"
2. servlet containers that buffer servlet out and "set content length"
Care to give us an API for #1 and an example of #2
My practice is to set content length for all images and other resources when the length is easily found - such as image files.
Bill
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: when to use setContentLength()
 
Similar Threads
html processing
Problem filtering servlet response
Caching rendered html in jsp
Exception coming while transforming
CharResponseWrapper