The difference between Java forwards and redirects

  • 2020-04-01 03:31:55
  • OfStack

There is an important difference between redirection and forwarding: when forwarding is used, the JSP container will use an internal method to invoke the target page, the new page continues to process the same request, and the browser will not be aware of the process. In contrast, a redirect means that the first page tells the browser to send a new page request. Because when you use redirection, the URL displayed in the browser becomes the URL of the new page, and when you use forwarding, the URL stays the same. Redirection is slower than forwarding because the browser also has to make a new request. At the same time, the object in the request becomes unavailable after a redirect because a new request is generated by the redirect.

How do you choose whether to redirect or forward? Forwarding is usually faster and keeps the object in the request, so it's the first choice. But since the URL in the browser still points to the start page after forwarding, the start page will be called again if the current page is reloaded. If you don't want to see this, then choose to forward.

The difference between forwarding and redirecting

Don't use the session scope just to pass the variable to the next page. That would increase the scope of the variable for no reason. Forwarding might help.

Redirect: all variables stored in the previous request are invalidated and enter a new request scope.
Forward: the variables stored in the previous request will not be invalidate, as if the two pages were put together.


Related articles: