wood burning stoves*
The moose likes Servlets and the fly likes forward() and IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward() and IllegalStateException" Watch "forward() and IllegalStateException" New topic
Author

forward() and IllegalStateException

sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
The java docs say:

The forward method should be used to give another resource responsibility for replying to the user. If you have already accessed a ServletOutputStream or PrintWriter object within the servlet, you cannot use this method; doing so throws an IllegalStateException.

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Servlets9.html

Also, the return type for getOutputStream():
getOutputStream

public ServletOutputStream getOutputStream()
throws java.io.IOException


But in HF Servlets & JSP, on p. 209, there is this mock exam question:

1) 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

and they say the right answer is: B. flush (on p. 215). According to the java docs, another answer should be: D. getOutputStream. I tested getOutputStream() by putting this line:

OutputStream os = response.getOutputStream();

before a forward() in my code, and I got an IllegalStateException. So, it appears that HF Servlets & JSP is in error. Can anyone else confirm that?
[ October 14, 2006: Message edited by: sven studde ]
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Check the errata page of HFS in oreilly.com. I remember there is a comment related to this question.
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
you usually do not call a outputstream in a jsp. the jsp has an implicit Jspwriter object. and the question is related to that i believe. it states in the errata that both write and flush methods will lead to a state exception. so if you use a out.write() or out.flush() you get a illegalstate exception.


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Check the errata page of HFS in oreilly.com. I remember there is a comment related to this question.

Thanks. It say says:
[215] Answer to question 1;
Option C should also be checked.

-----

you usually do not call a outputstream in a jsp. the jsp has an implicit Jspwriter object. and the question is related to that i believe.

Were you under the impression that the following quote in my original post was just a brief synopsis of the actual question?
But in HF Servlets & JSP, on p. 209, there is this mock exam question:

1) 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

------------


it states in the errata that both write and flush methods will lead to a state exception. so if you use a out.write() or out.flush() you get a illegal state exception.

Using the code:

ServletOutputStream out = response.getOutputStream();

is a preliminary step to using:

out.write(aByteArray);

My tests (using Tomcat) show that just calling getOutputStream() is enough to cause an IllegalStateException, so I'm wondering if that is generally true or whether Tomcat is out of spec? The java docs say "accessing a ServletOutputStream" will cause the exception. I originally interpreted that to mean calling getOutputStream(), and my tests confirmed that getOutputStream() does cause an IllegalStateException. But maybe "accessing a ServletOutputStream" means calling one of its methods.
[ October 15, 2006: Message edited by: sven studde ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward() and IllegalStateException
 
Similar Threads
ejb
how to prepear exam --- EL language
Doubt on RequestDispatcher
HFSJ 2nd edition - Final Mock Exam incorrect answers!?
IllegalStateException not thrown!