6 Difference betwixt forward() together with sendRedirect() inwards Servlet - Java

Servlet inwards JEE platform provides 2 methods forward() together with sendRedirect() to road an HTTP asking to exactly about other Servlet for processing. Though, both are used for forwarding HTTP requests for farther processing at that topographic point are many differences betwixt forward() together with sendRedirect() method e.g. frontward is performed internally past times Servlet, but a redirection is a two-step process, where Servlet instruct the spider web browser (client) to become together with fetch exactly about other URL, which is dissimilar from the original. That's why forward() is likewise known equally a server-side redirect together with sendRedirect() is known equally the client-side redirect. Becuase of their usefulness, the deviation betwixt forward() together with sendRedirect is likewise a oftentimes asked Servlet interview question. Let's come across a duo of to a greater extent than differences to respond this enquiry better.

While working betwixt Servlet together with JSP, yous volition often purpose these asking re-direction methods similar include(), forward(), or sendRedirect(). Sometimes, yous volition include the answer from other Servlet together with sometimes it volition exactly frontward the asking for processing to exactly about other Servlet.

Btw, if yous are preparing for Java JEE interviews together with learning this equally business office of your interview preparation, yous should likewise cheque the Java Programming Interview Exposed, ane of the rare majority which covers all of import topics for Java JEE interviews. It covers marrow Java, Spring, Hibernate, JDBC together with other advanced topics expected from both beginner together with experienced Java programmers.

 Servlet inwards JEE platform provides 2 methods  half dozen Difference betwixt forward() together with sendRedirect() inwards Servlet - Java



Forward vs SendRedirect inwards Servlet

Now, let's come across exactly about of import differences betwixt sendRedirect() together with forward() method of Servlet API:


1) First together with most of import deviation betwixt the forward() together with sendRedirect() method is that inwards instance of former, redirect happens at server halt together with non visible to client, but inwards instance of later, redirection happens at customer halt together with it's visible to client.

2) Another fundamental deviation betwixt forward() together with sendRedirect() is that frontward is marginally faster than the redirect. The sendRedirect() is marginally slower than a forward, since it requires 2 browser requests, non one. See Head First Servlet together with JSP for to a greater extent than details.

3) Third deviation betwixt forward() together with sendRedirect() method is that inwards instance of forward() master URL remains intact, spell inwards instance of sendRedirect() browser knows that it's making a novel request, together with thus master URL changes.

 Servlet inwards JEE platform provides 2 methods  half dozen Difference betwixt forward() together with sendRedirect() inwards Servlet - Java
4) In the instance of forward, Any browser reloads of the resulting page volition only repeat the master request, alongside the master URL. While inwards the instance of sendRedirect() A browser reloads of the instant URL, volition non repeat the master request, but volition rather fetch the instant URL.

5) In social club to purpose the forward() method, Both resources must live on business office of the same context (Some containers brand provisions for cross-context communication but this tends non to live on real portable). But the sendRedirect() method tin live on used to redirect users to resources that are non business office of the electrical flow context, or fifty-fifty inwards the same domain. See Head First Servlet together with JSP for to a greater extent than details.

6) In the instance of forward, Since both resources are business office of the same context, the master asking context is retained. On the other hand, since mail redirect involves a novel request, the previous asking orbit objects, alongside all of its parameters together with attributes are no longer available after a redirect. (Variables volition demand to live on passed past times via the session object).


That's all most  the difference betwixt forward() together with sendRedirect() inwards Servlet. It's ane of the of import concepts to larn for Java spider web developers. It's non exactly of import from the interview indicate of sentiment but likewise from writing real-world Java spider web application using Servlet, JSP, jQuery together with other JEE technologies. Practically, yous volition convey several choices for  redirecting HTTP asking e.g. yous tin produce together with thus past times using jQuery equally well, but yous demand to purpose sendRedirect() together with forward() if yous desire to produce together with thus from Servlet.

Further Learning
Java Web Fundamentals By Kevin Jones
Introduction to Spring MVC

Subscribe to receive free email updates:

0 Response to "6 Difference betwixt forward() together with sendRedirect() inwards Servlet - Java"

Posting Komentar