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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
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: 12803
    
    5
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
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: 472

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.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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://aspose.com/file-tools
 
subject: when to use setContentLength()