• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot forward after response has been committed

 
Udayaditya Barua
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Udayaditya Barua
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic