File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Choosing Between Forward and Redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Choosing Between Forward and Redirect" Watch "Choosing Between Forward and Redirect" New topic

Choosing Between Forward and Redirect

Tauri Valor
Ranch Hand

Joined: Aug 03, 2005
Posts: 180

Often I found folks finding it messy choosing between Forward and Redirect..

I found this to be very clear:

When you want to pass the control from one page to another, you can either forward to the other page, or redirect to the new page using the sendRedirect() method of the implicit response object.

Notice the following differences between a forward and a redirect

When you forward, the target page is invoked by the JSP container through an internal method call; the new page continues to process the same request and the browser is not aware of the fact that more than one page is involved.

When you redirect, it means that the first page tells the browser to make a new request to the target page. The URL shown in the browser therefore changes to the URL of the new page when you redirect, but stays unchanged when you use forward.

A redirect is slower than a forward because the browser has to make a new request.

Another difference is that request scope objects are no longer available after a redirect because it results in a new request. If you need to pass data to the page you redirect to, you have to use a query string and pass them as request parameters (or save the data as session or application scope objects).

And now how do you decide if you should use forward or redirect?

Look at it like this: Forwarding is always faster, so that's the first choice. But since the URL in the browser refers to the start page even after the forward, you ask yourself what happens if the user reloads the page or just resizes the window, this often reloads the page automatically).

But if your start page is a page that updates some information, such as adding an item to the shopping cart or inserting information in a database, you don't want it to be invoked again on a reload. To show the result of the processing, redirect to a page with a confirmation message, instead of using forward.

Su Yeu
[ January 16, 2006: Message edited by: Su Yeu ]

A Moment's insight is sometimes worth a Life's experience.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Redirect After Post shows a nifty application of redirect. It's a full article expansion on your last paragraph, really.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Choosing Between Forward and Redirect
It's not a secret anymore!