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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "IllegalStateException" Watch "IllegalStateException" New topic
Author

IllegalStateException

Ashutosh Arya
Ranch Hand

Joined: Oct 07, 2008
Posts: 69
Hi,
I am getting the below exception whenever I forward the control to a new JSp from servlet.
Please suggest a solution.

java.lang.IllegalStateException: Cannot forward request if ServletOutputStream or PrintWriter has already been obtained

This is coming whenever I try to forward the request to a new page although its not hampering the flow of the application.

Is it due to the use of request dispatcher?

Please help me out!

Aashu
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

Ashutosh Arya wrote:I am getting the below exception whenever I forward the control to a new JSp from servlet.
Please suggest a solution.

java.lang.IllegalStateException: Cannot forward request if ServletOutputStream or PrintWriter has already been obtained

This is coming whenever I try to forward the request to a new page although its not hampering the flow of the application.

Is it due to the use of request dispatcher?

Can you post that code snippet here? It would be easier to see what's wrong.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

It means you're probably doing a lot of things in your JSP that you should not be. It's very rare to need to forward from a JSP. That you are trying to do so may indicate a design flaw.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Samuel March
Ranch Hand

Joined: Oct 28, 2009
Posts: 39
You need to decide at the top of the page before anything is started to be printed whether to redirect.
However, I believe that just like with tag classes , with JSP scriptlet or JSTL you can cancel page processing, then redirect, but that may be the same clause(meaning you could have processed a large portion of the page but not sent it).
Look through the <@page > directives for buffer , and i think one directive has an attribute output whether to be "verbose"(output as it occurs) also.
If you need to do that after print, you could cease at an appropriate element and print a javascript with
<script>
window.location="http://mysite/thepage.jsp";
</script>


...did you have the fish?!............................ No.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalStateException
 
Similar Threads
Advantages of redirect over forward
HF Final Mock Q27
using saveToken() and isTokenValid() methods in struts
forward() method of RequestDispatcher vs sendRedirect() on Response object
Puzzle:Difference of redirect method?