Solved

:java.lang.IllegalStateException: Cannot forward. Response already committed. for jsp: forward

Posted on 2011-09-30
20
2,197 Views
Last Modified: 2012-05-12
Hi all,
I am having the same issue. Can some one please help me fix this ?

I want to check for a particular user input. If the user input is entered wrong, I want to send the error message back to the form JSP.I tried using both of the follwing codes, but each time getting :java.lang.IllegalStateException: Cannot forward. Response already committed.
      at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:157)

Please find my code snippets below :
_______________________________________________________________________
if (userResponse.isValid()) {
  // process the form values
}else{
String errormessage="The text does not match,please try again";
                       session.setAttribute("errormsg",errormessage);  
String strURL = request.getRequestURI();
                         %>                        
                       <jsp:forward page="<%=strURL%>" />                      
                     <%
                     return;                          
                      }  
_____________________________________________________________

if (userResponse.isValid()) {
  // process the form values
}else{
String errormessage="The text does not match,please try again";
                       session.setAttribute("errormsg",errormessage);  
String responsePage =request.getRequestURI();
                     // out.print("responsePage"+responsePage+"<br/>");                    
        RequestDispatcher dispatcher = request.getRequestDispatcher(responsePage.toString());
                       dispatcher.forward(request, response);                        
                      }
________________________________________________________________________

Thanks.
0
Comment
Question by:Gopal2
  • 6
  • 5
  • 5
  • +1
20 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36895668


Please, read detailed explanation of the nature of this error and recommendations herre:
http://www.jguru.com/faq/view.jsp?EID=501393
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895674


As I undersatnd, what this explanation in the link above mostly means is that you are doing this check somehwre late within the processing of your
request when some stuff was already committed to be sent to the client as part of the new page.
So maybe one of the possible solutions would be to move this check earlier in the page so that nothing would be written as response
before you make a decsion to redispatch it back to original page for correction.
Another recommendation is to try to process everything in java beans or in java methods and store stuff for future sending to response,
before any commitments to response are made (see below the releavnt sentence form the link above).

What this all means is that the Servlet API is inadequate as a general framework for sending messages among active objects to form a data pipeline. Fortunately, you have an API that is perfectly adequate for that task: Java itself. Structure your application to use JavaBeans and Java method calls. Restrict your Servlets to two types: one type is all data-processing, and the other type is all response-writing.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36895734
There is no need to introduce a framework to handle simple http processing.  The servlet API is perfectly adequate as a framework and is used worldwide.

"IllegalStateException: Cannot forward. Response already committed." means that the servlet or JSP page has already written something to the output stream (usually called "out").   If you have any HTML in your JSP before it checks the user input, or if you have any out.println statements which execute before you try to forward, then you'll get this message.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36895746
"Framework: Java itself" - was certainly a figure of speech from  http://www.jguru.com/faq/view.jsp?EID=501393

They certainly meant all the same thing which I mentioned in my explanation above, as well as mrcoffe : you should do this checking  of the input and, if necessary, call RequestDispatcher before you write any response to the output stream from the current page.
0
 

Author Comment

by:Gopal2
ID: 36927101
Thank you all for jumping in and offering solutions/suggestions.

I checked and fixed my code by commenting out all my out.print() statements,System.out.ptinln()
statements,in fact I also eliminated all the carriage returns everywhere possible.
In general, cutting down to bare minimum any code before this redirect statement.
Also added out.flush() ; right before rd.forwar(req,resp,page).
I tried all these below statements too:
                  <% out.flush();
                      rd.include(request, response);                        
               // if (rd!= null)  rd.forward(request, response);                      
                      //rd.forward(request, response);
                      //response.sendRedirect(responsePage);
                 // response.sendRedirect(response.encodeRedirectURL(responsePage));    
                  %>            
However, none of this have helped me over come the issue.Only one different error I see is ,
for rd.include(request, response) , I see :
Error 404: SRVE0190E: File not found: /wps/portal/../../../FirstPage  
where is the above url is correct and the required page exists at that path.

Still unable to find a solution to this issue.
Any lead in this direction would be highly appreciated.

Thanks.
                     

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36927629

>However, none of this have helped me over come the issue.Only one different error I see is ,
>for rd.include(request, response) , I see :
>Error 404: SRVE0190E: File not found: /wps/portal/../../../FirstPage  
>where is the above url is correct and the required page exists at that path.


didn't understand - do you still see IllegalStateException or you now see different exception ?
0
 
LVL 27

Expert Comment

by:rrz
ID: 36928507
>Also added out.flush() ; right before rd.forwar(req,resp,page).  
Don't call  out.flush() .   It will cause response headers to be sent and commit the response.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928514
I also thought this way, but it looks like no IllegalStateException is now obsereved
0
 

Author Comment

by:Gopal2
ID: 36931979
Hi for yan,
"for rd.include(request, response) , I see :
>Error 404: SRVE0190E: File not found: /wps/portal/../../../FirstPage  
>where is the above url is correct and the required page exists at that path.
"
  It is new exception, that is not coming up in the log file, but gets thrown on to the JSP where I have this logic.

rrz@871311:"Don't call  out.flush() .   It will cause response headers to be sent and commit the response." Did that, still no change.I get the same error message as above.

Thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36932015
But this is a new and different issue
0
 
LVL 27

Expert Comment

by:rrz
ID: 36932272
Please post a complete test page that we can test.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36932508
>> rd.include(request, response)

If you want our help with the original question, then remove this line -- it's causing the exception and it's likely an easy fix -- you probably have the url wrong for your included file.

If you do want our help with the new exception, then you have to post the code which creates rd and sets the path.
0
 

Author Comment

by:Gopal2
ID: 36933177
Please take a look at the attached file with code.

One clarification though.
I tried using
rd.forward() -- getting "IllegalStateException: Cannot forward. Response already committed."
rd.include() -- getting no error log, but the following gets printed on this JSP:"Error 404: SRVE0190E: File not found: /wps/portal/../../../FirstPage  "
I see that that the URL is correct.

Thanks.




spamfilter.doc
0
 
LVL 27

Expert Comment

by:rrz
ID: 36934742
You should paste your code into the code box of the comment form provide here. No need to create a Word doc.
In regard to your original problem, I don't see any reason for the IllegalStateException.  
Since the code you posted starts with
><p></p>  
I am guessing that  there is more to the page or you are including the posted code into another page.
What is it ?  
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36937043
rrz@871311 -- it turns out that the asker is using a captcha library, and this fragment actually runs as a jsp page.  It will only really work if another page -- with the captcha image on it -- is the submitter to this page, of course.

In fact, I can't make it break.  So my guess is that the "firstPage" parameter is being set incorrectly.  Or something else is wrong with it.

Gopal2: you have some typos in what you posted, but since you wouldn't even be able to compile with them, I assume that your version doesn't have them.
0
 
LVL 27

Expert Comment

by:rrz
ID: 36937504
Thanks for the explaination, mrcoffee. I think you are right.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 36937581
Gopal2: I'll reiterate, in case my previous post was unclear.

When I run your code, setting the various request parameters and fixing a few typos so it will compile, your code runs.  I can put in a valid url for the firstPage parameter, and the page redirects to it successfully.

So I think that your firstPage parameter value being passed to the page is incorrect.
0
 

Author Comment

by:Gopal2
ID: 36945386
Hi mrcoffee365,

   I did run the exact code and got the 404 exception.I am unable to figure out how the url I'm giving is wrong.I tried absolute URL hard coded , then relative URL from web context, then,request.getRequestURI() .

When I try printing out the URL value, t gives me the correct URL value.
But still throws 404 error.Baffling.

Can you please give me the exact url you are passing to the rd.include() method?


Thanks for all the help.
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 36946027
I use a local url that works in my test server.  So it won't help you.

But it is of this type:
/ee/simpletest.jsp

I call the page in the browser with request parameters that look like this because I don't want to spend the time setting up the real captcha image:
?recaptcha_challenge_field=test&recaptcha_response_field=test&firstPage=/ee/simplest.jsp

All I can say is that called that way, the page executes and sends me to the firstPage value.  

0
 

Author Closing Comment

by:Gopal2
ID: 36950750
Hi all,

Actually I am working in Portal/ILWCM/WebSphere environment.I saw that the environment is appending a specific String to what ever URL I was trying to redirect to.  So , as such, the rd.forward(),include(), etc., will not work for my scenario.

Therefore I had to entirely steer away from this approach and am trying something else altogether.However,your opinions/posts helped me understand what's going on.

  Thank you all very much for keeping this discussion going on relentlessly.
  Regards.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read our guide on how to survive being on-call.
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question