20 Differences forward vs sendRedirect Methods

20 Differences forward vs sendRedirect Methods


We have seen earlier include() and forward() methods of

RequestDispatacher interface with example programs and figurative explanation. 15 differences were shown between include() and forward().

sendRedirect(String URL) is discussed with example program in sendRedirect Method Example in Servlet.

This posting forward vs sendRedirect discusses the difference between forward() and sendRedirect() methods.

Let us tabulate forward vs sendRedirect differences.
Property forward() sendRedirect()
Defined interface Defined in RequestDispatcher Defined in HttpServletResponse
Signature void forward(ServletRequest request, ServletResponse response) void sendRedirect(String url)
Client awareness Client is not aware of that he is getting response from a different Servlet as the URL will not change in client’s browser. Client can know easily as the URL (from where he is getting response) changes in the client browser’s prompt.
Execution control Execution control changes to another Servlet on the same server without client being informed that altogether a different Servlet is going to process his request. Control changes to client
Where is what? Forward is done on server side without client’s knowledge. Browser issues a new request on the URL that is redirected (sent as parameter) by the server and client can easily aware of.
Where happens Everything happens on server side within the Web container and client is not involved. sendRedirect() causes the Web container to return to the client’s browser. Client inturn can redirect to different servers or domains.
Speed Faster as forward runs on server-side entirely and no extra network trip (to client) is required. Due to extra round trip between browser-server-browser (running on client as well as on server side), it is slower.
Content forward() sends the same request to another resource of the same Web application. Calls another page with a different request URL but not on the same request.
Usage Used when processing is done by another Servlet Used when wanted to redirect the client request to another Web site (completely out of current context) or to redirect the errors to another resource
Reusability forward() reuses the current request object Redirects create a new request object; consequently looses the original request with all its parameters and attributes.
Transfer of parameters Original request and response objects transfer data coming from client along with additional information set with setAttribute() method (if any) to another resource request and response objects. Redirect action sends header back to the client. Browser uses the URL contained in the header to call a new resource. As client initiates a new request, the original request and response objects are lost and fresh ones are to be created.
Transfer control Internally, the Servlet container transfers control of client request to another Servlet (or JSP). This method sends the HTTP response to client browser to allow the client to send another request with a different URL. Usage of this method is equivalent to opening a new browser window and typing the URL.
What is sent? Server sends the response (information required) to the client. With this method, server sends a URL to the client.
Visual difference Client cannot see the address of new resource that honours the client request in the address bar of the browser. Client can see the new redirected address in address bar.
Examples Calling another resource to process the data like validation of Login data. Calling advertisements on the Web page or payment gateways.
Task separation With this method, the responsibility of handling the client request can be distributed between many Servlets (or JSPs). Used to transfer control altogether to a different domain. Also used to write separation of tasks.
Back and Forward buttons As everything happens on server with forward, nothing is stored on browser history. So, Back and Forward buttons will not work. As client makes new request and updated in browser history, back and forward buttons work.
URL Use only relative URLs with forward(). Use absolute URLs.
MVC to hide Useful in MVC design pattern to hide JSP/Servlet from direct access. Once redirected to client, server looses control.
Which one to prefer? If you would like to forward the client request to a new resource on the same server for further process, prefer forward() where data of the original resource can be passed to the new resource. If you would like to transfer the control to a new server or domain where client treats as a new task, prefer sendRedirect(). If the data of the original resource (which client requested) is needed in the new resource, store them in Session object and reuse.

Some more points to notice of forward vs sendRedirect.

  1. Session is not lost in both cases.
  2. The above differences are applicable to Servlets and JSPs. In Servlets, these methods are used in service() and in JSP used in scriptlets.
  3. In frameworks like Struts, the Controller can decide, at the end of request processing, which one to use of either forward or redirect operation.
  4. The Controller also can decide with forward() method, to what resource the forward should be made, depending on different conditions of client request requirements.

Which one is preferred?

Just depends on the scenario.

If you would like to forward the client request to a new resource on the same server for further process, prefer forward() where data of the original resource can be passed to the new resource.

If you would like to transfer the control to a new server or domain where client treats as a new task, prefer sendRedirect(). If the data of the original resource (which client requested) is needed in the new resource, store them in Session object and reuse.

Leave a Reply

Your email address will not be published. Required fields are marked *