This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question (From Head First Servlets and JSPs)

 
Eurig Jones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateException? (Choose All that Apply)

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


The answer according to the book is B and C.

This question confuses me because:
- Question implies that the methods are part of RequestDispatcher. They aren't!
- The book states that it's flush you should only be worried about because it sends the response to the client. even though write is listed in the same piece of code.
- According to this link, getOutputStream would also cause an IllegalStateException

Is this an incorrect answer? If so, what is the correct answer?

Please help,
Eurig
[ August 29, 2008: Message edited by: Eurig Jones ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jones,

Whenever we do a forward using request dispatcher, container is suppose to clear the output buffer and then handover the request to another page.
Here IllegalStateException means we have already sent the response to client.
Genrally in the JSP page auto flush attribute is "true" which means you don't need to flush and also if on the writer object if you write the more than the size of buffer, output will be committed.
So if we take both the cases " write method can lead to Illegal state exception".

however on the servlet part if you flush the writer object after writing something, you will get the exception.

Getting a writer object will not cause the IllegalStateException. committing something on writer object will cause it.
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent sunny , I was also confused regarding the question.

best regards,
omi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic