Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on IllegalStateException (page 205 of HFSJ)

 
Claudio Lande
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to page 205 of HFSJ.

Page 205 deals with the matter of writing something on the response before executing RequestDispatcher.forward().

According to the text in the Bang! box, the servlet on top of the page is going to fail (and cause an IllegalStateException) because of the flush() method called on the output stream of the response before executing the forward(). But then exercise 1 (please see errata of the book) of the same chapter claims that also write()ing to a response causes the same exception.

If this is true, than page 205 should be corrected to point out that the IllegalStateException is raised whenever the servlet writes on or flushes the response output stream and then performs a RequestDispatcher.forward().

If this is not true, then what happens to the data already written in the output stream of the servlet before forwarding to another servlet/JSP? Is it cleared (not sent to the client) or is it sent to the client in the forward() call before the invoked servlet/JSP writes more data?

Thanks in advance!
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the data writtent to the Writer object, but is not flushed yet, then call to RequestDispatcher.forward() method will ignore this data.
The data can be flushed either by calling response.flush(), or when exceeding buffer size of the response. So, even if you didn't cal flush method, you can get IllegalStateException
 
Claudio Lande
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sergey,

thanks for your explaination.
I believe that the book should be clearer on the subject of "committing a response" (perhaps including your explaination).

Thank you again,

Claudio
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic