*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Inconsistency on page 215 of the book Head First Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Inconsistency on page 215 of the book Head First" Watch "Inconsistency on page 215 of the book Head First" New topic
Author

Inconsistency on page 215 of the book Head First

Erick Macedo
Greenhorn

Joined: Feb 17, 2013
Posts: 14
Good Afternoon Everyone, This is my first post here, I'm sorry my English, I am Brasileiro.

I'm studying for OCPJWCD by Head First book, the book has a question that is kind of inconsistent.

Page 215 response exercise 1.


In the version in Portuguese he speaks that B and C are correct.
In the English version he says that only B is correct.
Therefore, I had to test alternatives to the nail.
Really agent call the flush response is generated in an IllegalStateException, however if also call the write or just give the getOutputStream

is also generated an IllegalStateException

And now?, I can conclude that both versions of the book is wrong?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Do I miss something? RequestDispatcher just has 2 methods: forward and include.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Erick Macedo
Greenhorn

Joined: Feb 17, 2013
Posts: 14
That's right Roel De Nijs, he has only two methods.
However to interpret the question to believe that cite the methods read, flush, write, getOutputStream, getResourceAsStream he is referring to answer.

And if referring that to call these methods in response and then use the forward or include can be generated IllegalStateException.

I believe it is this ..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Based on the API only the forward-method can throw an IllegalStateException (if the response was already committed)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

And it is also mentioned in the errata list of this book.
Erick Macedo
Greenhorn

Joined: Feb 17, 2013
Posts: 14


Based on the API only the forward-method can throw an IllegalStateException (if the response was already committed)


By doing getOutputStream in the response already committed?

because if I do response.getOutputStream and then call forward, is also generated IllegalStateException

D is thus the correct
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi,

I agree that the question is a bit vague. What the writers meant is: What methods on the HttpServletResponse will cause the RequestDispatcher.forward() or RequestDispatcher.include() to throw an IllegalStateException.

B is correct, and C is incorrect according to the errata.

By doing getOutputStream in the response already committed?
because if I do response.getOutputStream and then call forward, is also generated IllegalStateException

No, this will not cause the response to be committed.

Tomcat has an unpredictable behaviour when it comes to writing to the response or getting the outputstream and the forwarding. In short: don't do anything with the response if you are going to do a forward. As the specs don't say what should happen in such a case: it might work but it also might not work...

Regards,
Frits
Erick Macedo
Greenhorn

Joined: Feb 17, 2013
Posts: 14

I agree that the question is a bit vague. What the writers meant is: What methods on the HttpServletResponse will cause the RequestDispatcher.forward() or RequestDispatcher.include() to throw an IllegalStateException.


It is now clear, is that the exam is badly formulated questions, like this?


No, this will not cause the response to be committed.

Tomcat has an unpredictable behaviour when it comes to writing to the response or getting the outputstream and the forwarding. In short: don't do anything with the response if you are going to do a forward. As the specs don't say what should happen in such a case: it might work but it also might not work...


Perfect, was this kind of answer I needed.

Never was able to discover it myself, because I tested with tomcat and actually using the response.getOutputStream (), and calling him forward generates IllegalStateException.

So I can tomcat says in some cases does not follow the spec?

Thank you

Erick Macedo
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

It is now clear, is that the exam is badly formulated questions, like this?

No, they are clear.

So I can tomcat says in some cases does not follow the spec?

Yes, this is what the specs say:

The Forward Method
The forward method of the RequestDispatcher interface may be called by the calling servlet only when no output has been committed to the client. If output data
exists in the response buffer that has not been committed, the content must be cleared
before the target servlet's service method is called. If the response has been
committed, an IllegalStateException must be thrown.

In other words, if there was something written to the response, the buffer should be cleared and then the forward method should work (so no throwing of Exceptions)
An IllegalStateException should only be thrown if the response was committed.

Regards,
Frits
Erick Macedo
Greenhorn

Joined: Feb 17, 2013
Posts: 14
In other words, if there was something written to the response, the buffer should be cleared and then the forward method should work (so no throwing of Exceptions)
An IllegalStateException should only be thrown if the response was committed.


I understand, then to say that the tomcat does not clear the buffer, and calls the Forward, so Exception

Thank
 
 
subject: Inconsistency on page 215 of the book Head First