aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes before committing the response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "before committing the response" Watch "before committing the response" New topic
Author

before committing the response

tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
hi friends,
i find there're many instances in which a methord is demanded to be called before committing the response. could anybody answer me what on earth does it mean by saying "call before committing the response"? what'll happen on the server?
thanks,
tony


SCJP2, SCWCD
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
A response is considered to have been committed if any part of the response has actually been sent to the client. You can check this programmatically by isCommitted() of the HttpServletResponse interface.
There are some methods that you must "call before the response is committed", like setContentType(), because they basically set the parameters that will frame the response. Calling them after the response is, well, committed will throw an IllegalStateException.
-anthony
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The content you generate is buffered (you can adjust the buffer size, by the way). As soon as you have generated more HTML than will fit in the buffer, the buffer contents will be sent to the client. This is called "committing the response".
The important point is that not just the buffer content is being sent - a HTTP response consists of a header (which includes, among other things, the response code and any cookies you set) and a body (the HTML). When the response is committed, the response header will be generated and sent to the client.
This is why you can't call a number of methods once the response has been committed: these methods need to manipulate the response header.
- Peter
tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
thanks peter & anthony
the messages are great helps to me to understand the topic.
tony
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898

Originally posted by Anthony Villanueva
Calling them after the response is, well, committed will throw an IllegalStateException.

I could not understand quite clearly why servlet cannot respond more than once?
I have read that even CGI applications, since it expensive to open each time a session/socket/connection, are able to maintain the old one for continuous use.
[ December 28, 2002: Message edited by: G Vanin ]
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
I wonder
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by G Vanin:
I could not understand quite clearly why servlet cannot respond more than once?
How can you respond more than once to a single request? If you need to send back multiple things then your response can use the MIME multipart type or similar.
A single request has by definition a single response. That doesn't mean the communication channel is closed after the response -- not if HTTP 1.1 keepalive is being used -- but that works at the transport level, you're not seeing any of it and it doesn't change the request/response paradigm. It is managed entirely by the container.
- Peter
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Thanks. I've got it
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: before committing the response