Discussions

Web tier: servlets, JSP, Web frameworks: sendRedirect() vs forward()

  1. sendRedirect() vs forward() (4 messages)

    Hi,
        What is the difference between the following?
    HttpServletResponse.sendRedirect(String url)
    and
    RequestDespatcher.forward(ServletRequest req,ServletResponse res)
    Are the both methods doing the same funtionaltiy?
    Thanks in advance
    Bye
    Rama

    Threaded Messages (4)

  2. http://forum.java.sun.com/read/16802606/qAN3e8yNe240AAgE8#LR
  3. sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response.

    forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.

    There are different situations where you want to use one or the other. For example, if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include(). If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect().

    Andy
  4. Hi Andy Nguyen,
                   Thanks for clearcut explanation.
    Bye,
    Rama.
  5. sendRedirect() vs forward()[ Go to top ]

    in the case of the sendRedirect(String) technique it will call on the response object,but in the case of the RequestDispatcher(String) it will work on the request object, here we having 2 tech.. i.e forward(request,response) and includ(request, response), i think this is basic difference b/t the both tech..... the final goal of 2 tech... are the same to bypass the request from one jsp page to another.
            thank u