aspose file tools*
The moose likes Servlets and the fly likes  Cannot forward after response has been committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " Cannot forward after response has been committed " Watch " Cannot forward after response has been committed " New topic
Author

Cannot forward after response has been committed

Udayaditya Barua
Greenhorn

Joined: Dec 05, 2008
Posts: 8
Hi, im kind of new to servlets. i have a servlet which accepts data from a HTML form. Basically 2 numbers, according to the option chosen, their addition,multiply etc. User enters the answer, servlet checks whether it was correct or not. If wrong, shows solution.
processRequest() is the function which is called by doPost. It calls processAnswer() which basically does the forwarding of the page according to the answer.
Heres processRequest

WHICH CALLS processAnswer()

Really need help here..I tried adding a "return" in processRequest, didnt help.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Hello and welcome to java ranch.

I am sorry, did not get your problem..
If "Cannot forward after response has been committed" is the problem then always remember that you can not forward the servlet/jsp to another one of a kind after you have committed the response to the client!! Remember that always, forward before you commit.

Hope this helps..


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Udayaditya Barua:




processAnswer(answer, opt, number1, number2, request, response);

why you put after catch ?...i guess first it enters catch and forward to some page agin it calls processAnswer and init once agin you are trying to forward .... instead you put your processAnswer(answer, opt, number1, number2, request, response); inside try block
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
hi,
Do remember that in switch case once a condition is met in a case all the subsequent case blocks get executed ,hence the error .To make it work find
an alternative to switch case approach.Try using if ...else I think it might
work out
[ December 06, 2008: Message edited by: Abhijit Rai ]

SCJP5 ,SCWCD5
Udayaditya Barua
Greenhorn

Joined: Dec 05, 2008
Posts: 8
@shekhar
thanks

Ok, sry for the delay.. Managed to solve the problem after a lot of searching... Had to write a 'return' after the forward command..
About the switch case executing every statement, doesnt writing a "break" solve that?..Just asking..
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Yes of course. You know the fundamentals of switch case statement.....
By writing break you tell the program to come out after the case has matched....
Otherwise once a case is matched and break is not used then all the subsequent cases too are evaluated.... Hope this helps.
 
Don't get me started about those stupid light bulbs.
 
subject: Cannot forward after response has been committed