Response.sendRedirect() and RequestDispatcher.forward() are the 2 main methods provided by java to pass the execution to next resource. In this post, I am going to discuss major differences between response.sendRedirect()
and request.getRequestDispatcher().forward(req, res)
method’s execution flow. Next to that, I want to explain When to use sendRedirect()
and when to use forward()
?
Differences between sendRedirect() and forward()
HTTPServletResponse Interface defines SendRedirect(). |
RequestDispatcher Interface defines Forward(). |
SendRedirect tells the browser to load redirected URL. Browser creates new request to load redirected URL. |
Forward passes the control of current request to next resource (e.g. servlet or jsp page) on the same server. Servlet container forwards the same request to next resource. Browser is not involved in forwarding the request. |
Redirected URL can be on the same server or on another server. |
Forwarded URL must be on the same server. If forwarded URL is not found on the same server, then server will respond as 404 Not Found error to the browser. |
Browser is involved in redirecting to next URL using sendRedirect() method. |
Browser is unaware of forwarding to next URL using forward() method. You can not see the forwarded URL address in browser. |
Browser creates new request to get next URL in sendRedirect() method. Consider saving request parameters from previous request in session, so that saved data can be accessible in new request on the server. |
Servlet container passes same request to next resource for further processing in forward() method. Request parameters can still be accessible in next resource. |
Status code 302 is sent to browser to indicate redirection in sendRedirect() method. |
Final response is sent to browser in forward() method. |
URL is changed and updated to last resource loaded by the browse in browser’s URL bar. |
URL is not changed and reamin to the first resource called on the server in browser’s URL bar. |
Execution Flow of HTTPServletResponse.sendRedirect() Method
Consider a login page scenario. If user logs in then redirect the request to welcome.jsp page. Following image depicts the interaction between browser and server.
Execution Flow of RequestDispatcher.forward() Method
Consider a login page scenario. If user logs in then forward the request to welcome.jsp page. Following image depicts the interaction between browser and server.
When to use sendRedirect() vs forward()?
If single operation involves multiple jsp pages and servlets and all of the resources are on the same server, then use RequestDispatcher.forward() method to pass the control of request to next resources on the same server.
If you need to redirect request to other domain or another server then use HTTPServletResponse.sendRedirect() method to transfer the control of request to next resource. Browser will create a new request and redirect the page to new URL.
If you are using HTTPServletResponse.sendRedirect() to redirect URL on the same server more than certain threshold, then sometimes browser interprets it as like automated behaviour or infinite looping behaviour and breaks. It is general practice to use forward() in such scenario to avoid issues in production.