
package p;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Ser extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
String location= null;
if(1<2){
location="/j1.jsp";
RequestDispatcher re=getServletContext().getRequestDispatcâ?¦
re.forward(req, resp);
}
location="/j2.jsp";
RequestDispatcher re=getServletContext().getRequestDispatcâ?¦
re.forward(req, resp);
}
}
this servlet is calling by a jsp..there is no problem,in this servlet the if condition always getting true coz 1<2...so the programme counter goes through the loop...and when the counter executes the line re.forward(req, resp);..it it redirect to jsp name j1....(as i knew when ever re.forward(req, resp); is execute the counter will move to the last location which is stored in string obj...but this is not happening coz after executing the re.forward(req, resp); inside the if loop it comes out and also execute the last portion of the code...and giving error
i thought re.forward(req, resp); works like return means jumps to the response ...so rest of the code will not be executed
if any any one knows plz tell me what happens when the first re.forward(req, resp); executes...and what whappen when the last one also execute
ERROR
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.IllegalStateException: Cannot forward after response has been committed
p.Ser.doGet(Ser.java:23)
javax.servlet.http.HttpServlet.servicâ?¦
javax.servlet.http.HttpServlet.servicâ?¦
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.