What is the difference between sendRedirect and requestDispatcher.forward?

Asked

Viewed 10,730 times

10

What is the fundamental difference between the use of methods

response.sendRedirect("Alguma pagina");  

and

RequestDispatcher despachar = request.getRequestDispatcher("/Alguma pagina");
despachar.forward(request, response);

since "they do almost the same"?
As far as I know sendRedirect() cancels the current request and starts a new request.

1 answer

11


The difference is between redirect the customer to a page (sendRedirect) and forward a request to be met by another resource (forward).

In the first case (sendRedirect), the client will receive an http response at which header there will be information that he must order another page, and the browser will make this request. That is, the redirection takes place on the client side.

In the second case (forward), on the server side the user request will be forwarded to be met by another resource (other Servlet). This other Servlet eventually return another page to the user.

The difference is quite large especially when it comes to user experience.

Unfortunately there are many systems that use request forwarding when they should use redirect.

Examples of misuse of forward

The user clicks to save an edit (makes a Submit of the page). Then the server saves the data in the database and forwards (forward) the user to the page that displays for reading the newly edited record.

In the browser, the user made a Ubmit and as a response received the content from another page. The browser renders the content received but does not record that it is on another page and does not update for example the URL.

One of the problems with this is that the user will not be able to save the log view page for reading to his favorites. For the browser it is as if the user is still on the editing page. This also limits the use of the "back" and "forward" buttons of the browser, which will not offer the behavior that the user is used to.

Web experience

The experience proposed by the Web and that is usually expected by users, is that any Web system is composed of navigation between pages. Each feature is a different page, with its own URL, and you can go back to a previous resource and proceed again to the resource where it was before, you can save resources in the favorites to come back to them after easily, you can share these features by sharing Urls, etc..

Even the web systems that offer the experience single page application, if well designed, provide the user with this experience of browsing between pages. For example: gmail, google maps, facebook...

In these systems, the page is rarely or perhaps never fully reloaded, but only part of the content is redesigned. This improves the user experience but there is a huge concern so that he doesn’t miss out on that other basic experience of having the URL updated and then being able to access specific features via the back button or a URL saved in the favorites or shared with him by another user.

Use forwarding (forward) instead of using redirect (sendRedirect) kills this experience and does not motivate the developer to improve in web development because he gets used to taking resources on the server between a page and another, when it should mainly use the information contained in user requests and save very little or no status on the server.

  • it would be of great help to know some more problems of using forwarding instead of redirecting.

  • 1

    @Very interesting, thank you!! I’ll stay tuned for the Bookmark bid!

  • 1

    @Caffé, thank you very much for the enlightening answer, was just with this problem of not updating the URL when returning from Servlet and I managed to solve following his tip. Simply swap: request.getRequestDispatcher(). forward(".jsp file"); request.setAttribute("objObject", object); Por: Resp.sendRedirect(request.getContextPath() + "/resource/" + Object.getId());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.