Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward() and IllegalStateException

 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the errata page of HFS in oreilly.com. I remember there is a comment related to this question.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic