I have some problems with redirection in jsp app.
My redirect method is like this:
public static void redirectUrl(String url,HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
request.getSession().getServletContext().getRequestDispatcher("/" + url).forward(request,response);
}
When I start the app on localhost everything works fine but when I deployed it on the server it crash with this exception:
Servlet error
java.lang.IllegalStateException: Response has already been committed
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.EvermindHttpServletResponse.resetBuffer(EvermindHttpServletResponse.java:1933)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:221)
at app.framework.request.Controller.doPost(Controller.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:835)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:341)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:230)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.GetParametersRequestDispatcher.forward(GetParametersRequestDispatcher.java:257)
I put return statement after every redirect call but it won’t work.
Can someone tell me why?
Thanks in advance.
This error comes when response has already written.
See the answer in this post.
The term redirecting” you are using is not redirect as per your code.Its called “forwarding”.
Try:
EDIT (DetailedExplanation) :
request.getRequestDispatcher(“url”)means the dispatch is relative to the current HTTP request.The path parameter doesn’t have to start with a “/”
getServletContext().getRequestDispatcher(“url”)means the dispatch is relative to the root of theServletContext.The path parameter has to start with a “/”