File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Ilegal State Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Ilegal State Exception" Watch "Ilegal State Exception" New topic

Ilegal State Exception

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Hello all ,
can you please tell me regarding the illegal state exception.
if i have a jsp which has out.println and then i have a<jsp:forward> will it cause a illegal state exception.
if i have a jsp which has a<jsp:forward> and then out.println will it cause a illegal state exception.

i suppose there will not be any illegal state exception for <jsp:include> in either cases and does this apply same to the servlets when we use
RequestDispatcher.forward()(since <jsp:forward is same as RequestDispatcher.forward) and it has no effect for RequestDispatcher.include(also <jsp:include> is similar to RequestDispatcher.include() I suppose).
thnaks for the help

Han You

Joined: Oct 26, 2003
Posts: 25
I'm not sure, but I think both an out.println before or after a forward of any type will cause an IllegalStateException. The reason for this is, because an out.println would cause the server to send a response to the client, which would be weird, because the server also has to forward at the same time. (But then again, I'm not sure ) so it might be better to double check what I just said
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

That's basically right. When you forward, you are saying that the current JSP is giving up it's rights to output to the client. It's like saying "This is not the page you are looking for", while pretending that it is. eg This is useful if validation fails - they ask for details from the details screen, but the details screen forwards them to the validationError screen instead.
What this means is that if the JSP that forwards also tries to output data, whether before or after the forward call, this is an illegal action. It isn't allowed to both return data and call 'forward'.
This may not always result in an exception though. Data sent to the client gets buffered, so if the buffer hasn't been sent yet, the server will clear the buffer and pretend that the out.println() didn't happen. It's best not to rely on the buffer though, a better design is to decide whether you are going to foward before you output any data to the client.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
can you just give me a piece of code which gives the IllegalStateException when written because i have been writing code in the jsp which never gives that exception so it is like i am sort of not sure as to ito does really happen or not, i.e the exception occurs or not. why and how does the data gets buffered without sending that to the client?
please give an example.
I agree. Here's the link:
subject: Ilegal State Exception
It's not a secret anymore!