Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException

 
Ashutosh Arya
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Samuel March
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic