0

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.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69

1 Answers1

1
java.lang.IllegalStateException: Response has already been committed

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:

request.getRequestDispatcher("/" + url).forward(request, response);

EDIT (DetailedExplanation) :

request.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request.

RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp");

The path parameter doesn't have to start with a "/"

getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext.

RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");

The path parameter has to start with a "/"

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • Yes I have already saw this post but doesn't help me. Why? Because I found all place where I use rediect method and put after it return. I think something else isn't not right. – Jordan Borisov Jul 26 '12 at 08:04
  • @JordanBorisov: Can you provide some more code.The code which you have provided is not sufficient to identify the cause. – Hardik Mishra Jul 26 '12 at 08:36
  • OK I think I found the problem I have to use request.getRequestDispatcher... to work properly : see the url bellow: http://geekexplains.blogspot.com/2008/06/difference-between-forward-and.html – Jordan Borisov Jul 26 '12 at 09:08
  • @JordanBorisov: Good to hear that. I have added a difference of what you were trying previous. – Hardik Mishra Jul 26 '12 at 09:42