Sunday, January 14, 2024

6 Difference between forward() and sendRedirect() in Servlet - Answer

Servlet in JEE platform provides two methods forward() and sendRedirect() to route an HTTP request to another Servlet for processing. Though both are used for forwarding HTTP requests for further processing there are many differences between the forward() and sendRedirect() method e.g. forward is performed internally by Servlet, but the redirection is a two-step process, where Servlet instructs the web browser (client) to go and fetch another URL, which is different from the original. That's why forward() is also known as a server-side redirect and sendRedirect() is known as the client-side redirect. 

Becuase of their usefulness, the difference between forward() and sendRedirect is also a frequently asked Servlet interview question. Let's see a couple of more differences to answer this question better.

While working between Servlet and JSP, you will often use these request re-direction methods like include(), forward(), or sendRedirect(). Sometimes, you will include the response from another Servlet and sometimes it will just forward the request for processing to another Servlet.

Btw, if you are preparing for Java JEE interviews and learning this as part of your interview preparation, you should also check the Java Programming Interview Exposed, one of the rare book which covers all important topics for Java JEE interviews. It covers core Java, Spring, Hibernate, JDBC, and other advanced topics expected from both beginner and experienced Java programmers.




Forward vs SendRedirect in Servlet

Now, let's see some important differences between the sendRedirect() and forward() method of Servlet API:

1. First and most important difference between the forward() and sendRedirect() method is that in the case of the former, redirect happens at the server end and not visible to the client, but in case of later, redirection happens at the client end and it's visible to the client.

2. Another key difference between forward() and sendRedirect() is that forward is marginally faster than the redirect. The sendRedirect() is marginally slower than a forward since it requires two browser requests, not one. See Head First Servlet and JSP for more details.

3. Third difference between forward() and sendRedirect() method is that in the case of forward() original URL remains intact, while in the case of sendRedirect() browser knows that it's making a new request, so the original URL changes.

forward() vs sendRedirect() in Servlet Java


4. In the case of forwarding, Any browser reloads of the resulting page will simply repeat the original request, with the original URL. While in the case of sendRedirect() A browser reloads the second URL, will not repeat the original request, but will rather fetch the second URL.

5. In order to use the forward() method, Both resources must be part of the same context (Some containers make provisions for cross-context communication but this tends not to be very portable). But the sendRedirect() method can be used to redirect users to resources that are not part of the current context, or even in the same domain. See Head First Servlet and JSP for more details.

6. In the case of forward, Since both resources are part of the same context, the original request context is retained. On the other hand, since send redirect involves a new request, the previous request scope objects, with all of their parameters and attributes are no longer available after a redirect. (Variables will need to be passed by via the session object).


That's all about the difference between forward() and sendRedirect() in Servlet. It's one of the important concepts to learn for Java web developers. It's not just important from the interview point of view but also from writing real-world Java web applications using Servlet, JSP, jQuery, and other JEE technologies. 

Practically, you will have several choices for redirecting HTTP requests like you can do so by using jQuery as well, but you need to use sendRedirect() and forward() if you want to do so from Servlet.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.