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

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 forward() and sendRedirect() method e.g. forward is performed internally by Servlet, but a redirection is a two-step process, where Servlet instruct 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 other 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.

Difference between forward() vs sendRedirect() in Servlet JEE

Forward vs SendRedirect in Servlet

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

1) First and most important difference between the forward() and sendRedirect() method is that in case of former, redirect happens at server end and not visible to client, but in case of later, redirection happens at client end and it's visible to 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 case of forward() original URL remains intact, while in case of sendRedirect() browser knows that it's making a new request, so original URL changes.

forward() vs sendRedirect() in Servlet Java
4) In the case of forward, 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 of 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 its 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 application using Servlet, JSP, jQuery and other JEE technologies. Practically, you will have several choices for  redirecting HTTP request e.g. 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.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets and JDBC for Beginners: Build a Database App

No comments:

Post a Comment

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