• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Why is the word "abbreviation" so long? And this ad is so short?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic