What is the difference between sendRedirect and requestDispatcher.forward?

10

What is the fundamental difference between using methods

response.sendRedirect("Alguma pagina");  

and

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

Since "they do practically the same"?
As far as I know, sendRedirect () cancels the current request and initiates a new request.

    
asked by anonymous 02.10.2015 / 20:52

1 answer

10

The difference is between redirect the client for a sendRedirect page and forward / strong> a request to be served by another resource.

In the first case ( sendRedirect ), the client will receive an http response whose header will tell you that it should request another page, and the browser will make this request . That is, redirection occurs on the client side.

In the second case, on the server side, the user's request will be forwarded to be serviced by another resource (another servlet ). This other servlet will eventually return another page to the user.

The difference is quite large especially in terms of the user experience.

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

Examples of misuse of forward

The user clicks to save an edit (makes a page submit). Then the server saves the data in the database and forwards the user to the page that it displays to read the newly edited record.

In the browser, the user made a submit and received the content of another page as an answer. The browser renders the received content but does not register 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 to the bookmark. For the browser it's like the user is still on the edit page. This also limits the use of the browser's "back" and "forward" buttons, which will not provide the behavior that the user is accustomed to.

Web Experience

The experience proposed by the Web and generally expected by users is that any web system is composed of navigation between pages. Each resource is a different page with its own URL, and you can go back to a previous resource and advance back to the resource where you were before, you can save resources on your favorites to return to them later easily, you can share these resources by sharing URLs, etc. .

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

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

Using forward ( forward ) instead of using redirect ( sendRedirect ) kills this experiment and does not motivate the developer to improve web development as he gets used to it resources on the server between one page and another, when it should mainly use information contained in user requests and save very little or no state on the server.

    
02.10.2015 / 21:23