File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question (From Head First Servlets and JSPs) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question (From Head First Servlets and JSPs)" Watch "Question (From Head First Servlets and JSPs)" New topic

Question (From Head First Servlets and JSPs)

Eurig Jones
Ranch Hand

Joined: Feb 05, 2008
Posts: 57
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,
[ August 29, 2008: Message edited by: Eurig Jones ]
Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 598

Excellent sunny , I was also confused regarding the question.

best regards,

Most of the programming problems can't be solved because you don't know what the exact problem is.
Defining your problem correctly leads you to the right solution.
I agree. Here's the link:
subject: Question (From Head First Servlets and JSPs)
jQuery in Action, 3rd edition