This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ]
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.