Difference between JSP forward and redirect

JspRedirectServletsForward

Jsp Problem Overview


Please explain the difference between jsp:forward and redirect.
What is happening in each case?

Jsp Solutions


Solution 1 - Jsp

  • redirect sets the response status to 302 [1], and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url

  • forward happens entirely on the server. The servlet container just forwards the same request to the target url, without the browser knowing about that. Hence you can use the same request attributes and the same request parameters when handling the new url. And the browser won't know the url has changed (because it has happened entirely on the server)


> [1]: This is an example of industry practice contradicting the standard. > The HTTP/1.0 specification (RFC 1945) required the client to perform a > temporary redirect (the original describing phrase was "Moved > Temporarily"), but popular browsers implemented 302 with the > functionality of a 303 See Other. Therefore, HTTP/1.1 added status > codes 303 and 307 to distinguish between the two behaviours. However, > some Web applications and frameworks use the 302 status code as if it > were the 303. Source

Solution 2 - Jsp

I've heard interesting explanations of redirect and forward. Imagine that you need some service from your friend. It doesn't matter what service. Suppose that your friend can't help you but knows who can.

He would REDIRECT your request if he would tell you: "I can't handle this, but I know who can. Here his phone number. Call him."

He would FORWARD your request if he would tell you: "No problem" and calls that man by himself without notifying you about involving another person in handling your desire. Then, your friend will get the result of sorting out your wish and transmit it to you.

Solution 3 - Jsp

Redirect is also slower compared to forward because it has to go through the browser and wait for the browser to make a new request, and also therefore causing request scope objects to be unavailable after redirect.

Solution 4 - Jsp

Redirect :

  1. User requests a resource.
  2. Response sent to the user.
  3. This is not the requested resource, this is response with HTTP code 302 and contains the URL of the requested resource.
  4. URL could be same or different from the requested URL.
  5. Client browser makes request for resource again with the new URL, this time the actual resource is sent to the user.

Forward:

It is the process of simply displaying the requested resource to the user. It happens entirely on the server side.

Solution 5 - Jsp

This post gives a really good explanation about forward vs redirect using a nice real world example.

> The milk man comes and asks for monthly payment to you in your house. > Here house is the container and you are a resource existing in the > container. Milk man is the client or browser. > > He asks for the monthly payment to you, this is the request made by > the browser to resource A. If you go inside your house and ask your > mother (another resource B inside the same container) for the cash and > come back and deliver to milkman this is called forward. > > If you ask the milkman to speak himself to your mother inside your > house or you ask the milkman to speak to your father who is in his > office (different domain) then this is called redirect.

Solution 6 - Jsp

+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
|                             Forwards   vs.                              |                                  Redirects                                  |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
| ServletContext.getRequestDispatcher(location).forward(request,response) | httpServletResponse.sendRedirect(location)                                  |
| Communication between pages directly                                    | Communication b/w pages are indirectly by extra round trip from HTTP client |
| Communication happens within web-container                              | Communication happens outside web-container                                 |
| Use same Request and Response Object                                    | Use different Request and Response Object                                   |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+

Solution 7 - Jsp

When you forward a request,

-request and response objectsare transferred. -url stays the same.

When you redirect the request to another JSP/servlets,

-request and response objects are not transferred to new object. -Url changes to the directory of new page.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmmuView Question on Stackoverflow
Solution 1 - JspBozhoView Answer on Stackoverflow
Solution 2 - JspSerhii SobolievView Answer on Stackoverflow
Solution 3 - JsprmartinusView Answer on Stackoverflow
Solution 4 - JspNishantView Answer on Stackoverflow
Solution 5 - JsparavindaMView Answer on Stackoverflow
Solution 6 - JspPremrajView Answer on Stackoverflow
Solution 7 - JspBuskeekView Answer on Stackoverflow