• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

before committing the response

 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks peter & anthony
the messages are great helps to me to understand the topic.
tony
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I've got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic