There are multiple screens and servlets which together form a Java based web application. A JSP passes the control from one servlet to another servlet or JSP using two methods – Forward and Redirect. The Forward method forwards a request from one servlet to another resource in a web application and this resource can be another servlet, JSP page, or HTML file.
The Redirect method, on the other hand, redirects the request to a different application. You cannot do this with a forward method. If a redirect hits a different resource in the same application, then it is using different URL than the original request URL. If you don’t want to respond to a request, you can redirect the request to a different URL and the browser then sends the new request to the URL provided by you. This post highlights a couple of differences explaining the two methods in detail.
What is Forward?
In web-based systems or applications, there is often a need to transfer control between different resources or JSPs. For example, if you wish to place an order from an e-commerce site, you need to register first before proceeding. If you’re not yet registered with their system, the shopping cart may need to transfer control to the JSP form that is responsible for the registration process. The forward action is used specifically for this purpose. It is used to forward the request from one JSP to another or another resource within the same context.
What is Redirect?
This method is also used to forward HTTP requests but unlike a forward, it’s a two-step process where the redirect happens at the client-side to a different application altogether. The redirect method redirects the user to a new URL. The client’s browser automatically makes the new request on the URL specified in the redirect header that comes from the server. It requires a round-trip communication with the client, thus it’s relatively slower than a forward.
Difference between Forward and Redirect
Description of Forward Vs. Redirect
The forward( ) method is used to forward the request from one JSP to another or from one JSP to a servlet, or from one JSP to another resource in a web application. The control is passed internally by the container and the browser/client is not involved in the process. The forward method is declared in the RequestDispatcher. The SendRedirect( ) method is declared in HttPServletResponse and is used to redirect the client request to a different URL which is available on a different server or context. With a redirect, you can redirect the browser to a different application altogether.
Client Involvement in Forward Vs. Redirect
One of the key differences between the two methods is that web container handles all the processes internally in case of forward( ) and the URL doesn’t change in the client’s browser, so the client/browser is not involved leaving them completely unaware that the action has already taken place. In case of SendRedirect( ), the method sets the appropriate headers and body content to redirect the request to a different URL and the browser takes care of sending the new request to the URL which is visible to the client as the URL changes in the browser.
Execution Control
When the forward( ) method is executed at the request time, the current request gets forwarded to another JSP page and the processing of the current JSP is terminated. The request may be forwarded to a servlet written in the Java programming language, or to a static HTML page. A SendRedirect( ) request simply tells the browser to go to a different URL, sending the execution control outside the web application. It uses a two-step process to instruct the URL of the browser to make another request which transfers the control to client.
Speed
A forward( ) operates within the server and executes faster than a SendRedirect( ). A redirect has to go through the browser and then wait for the browser to make a new HTTP request. A redirect makes the server send the HTTP response status code 302 with a location header containing a new URL to the browser, and after the browser received the status code 302, it makes a new request to the URL found in the location header. This requires a round-trip communication with the client which makes it relatively slower than a forward( ).
Forward vs. Redirect: Comparison Chart
Summary of Forward Vs. Redirect
Learning the difference between the forward and redirect methods is one of the most important parts for Java developers. While a controller can perform either a forward or a redirect method at the end of processing a request, they have their own set of uses. Most of the time, you’d use a forward( ) method as it is marginally faster than a SendRedirect( ), which in fact, requires a round-trip communication with the client making it slower than a forward. Well, with a redirect, you can direct the browser to a different application. This cannot be done with a forward. In a nutshell, a forward works best when one component must perform business logic and share results with another component, while a redirect works best when the client should be redirected from one page to another.