• Post Reply Bookmark Topic Watch Topic
  • New Topic

when would calling setContentType be invalid  RSS feed

 
Davie Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From the servlet API for ServletResponse, isCommitted() checks if a response has been committed (has already had its status code and headers written).

setContentType() has no effect if called after the response has been committed.

So when/how does headers (and/or status code) get written? Is that done both implicitly and explicitly? By the servlet container or programmer?

Thanks,
dz
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HttpServletResponse object takes care of holding on to the header values as you set them. It then ensures that the headers are written before the body of the response.
Bill
 
Davie Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply!

Just trying to understand more...

I'm curious what triggers the writing out of HttpServletResponse object's header information. Does this happen after the servlet's "service" method call is complete? If so, when is calling isCommitted() used?

dz
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious what triggers the writing out of HttpServletResponse object's header information. Does this happen after the servlet's "service" method call is complete? If so, when is calling isCommitted() used?

I believe that is left up to the particular implementor since HttpServletResponse is just an interface. Why don't you delve into the Tomcat source code and find out?
Bill
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What usually triggers a commit is that the content you generate exceeds the capacity of the output buffer. See ServletResponse.[sg]etBufferSize and the jsp <@page> directive where you can specify a buffer size as well.

- Peter
 
Davie Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter!
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it in front of me now.

<%@page buffer="16kb"%>

Will set up a 16kb output buffer (probably by calling ServletResponse.setBufferSize(16384) under the hood). I seem to remember that the default is 8kb but don't quote me on this. [OK -- looked in the spec -- it requires a buffer of at least 8kb but specifies no upper bound -- PdH]

- Peter
[ August 21, 2004: Message edited by: Peter den Haan ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!