Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HF SCJP mock exam question: flush() vs. write()

 
Jean-Christian Imbeault
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 5, page 209, mock exam question #1 asks:

"When using a requestDispatcher, the use of which method can often lead to an IllegalStateException (choose all that apply):

A. read
B. flush
C. write
D. getOutputStream
E. getResourceAsStream

The correct answer is given as B, but I would have though the answer should have been B *and* C.

Couldn't writing 'theoretically' cause the response to be commited and hence cause and IllegalStateException if we try and forward the request?

Or is it in the specification that nothing is sent to the browser until flush() is called or the servlet's service method returns?
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The author of the question, probably, wants you to realize that a 'write' "often" implicitly does not lead to a 'flush'. Hence B is more correct than C.

>>Couldn't writing 'theoretically' cause the response to be commited and >>hence cause and IllegalStateException if we try and forward the request?
Good question. If you write a buffer > response.getBufferSize(), the OutputStream will automatically flush the stream, consequently leading you to IllegalStateException. So C can also be a correct answer. (not sure how to interpret 'often')

HFS authors allude to this on Pg 137, Para 3 of HFS book.
 
Jean-Christian Imbeault
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hum ... so I wonder if this is a trick question?

Hopefully this kind of question where a 'well it's theoritically possible' answer doesn't come up on the exam
 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's intended to be a trick question. The nice thing about the exam is that they tell you how many correct answers to choose. I believe they also try to avoid loose terms like "often" in the real exam questions (though I was not personally involved in writing those questions, so I can't say for sure).

(as an aside, in case anyone's paying extra close attention and matched my name to page xxxi of the book, I feel the urge to point out that I didn't write this particular question )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic