• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Committed Response

 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Someone pls. make me understand that when a response is said to be committed ?
Best wishes,
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A response consists of two parts: the header, and the body. The header contains such things as the response code (indicating success/failure/whatever) and the content length. The body is the HTML you create.
In servlets and JSPs, there is a buffer (normally 8k or so) for the HTML output. Once this buffer is full, the first chunk of the response body has to be sent to the browser. Obviously that means that the header also has to be sent. Once this is done, the response has been "committed". The header has been sent off and there's nothing you can change about it any more.
There are a number of methods that need to manipulate the header to do their job. This includes anything to set header fields, obviously, including setContentLength() and setContentType(). But it also includes sendRedirect(), which needs to send a TEMPORARY REDIRECT response to the browser. All these methods will throw IllegalStateException if they are called after the response has been committed.
response.isCommitted() will tell you if the response has been committed. You can use setBufferSize() to adjust your preferred buffer size; the larger the buffer, the more HTML you can generate before the response commits. But in general it is better to first do the behind-the-scenes work to parse the HTTP request and pull out the data, and only then generate the response. This comes naturally in a Model-View-Controller (MVC, Model II) architecture.
- Peter
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Thanx for a very detailed & clear explanation. It really worked...
Best Wishes,
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched anything about it in "Core Servlets..." and could not find. My search is bad ot the book?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic