• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inconsistency on page 215 of the book Head First

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I miss something? RequestDispatcher just has 2 methods: forward and include.
 
Erick Macedo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on the API only the forward-method can throw an IllegalStateException (if the response was already committed)
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it is also mentioned in the errata list of this book.
 
Erick Macedo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic