简体   繁体   中英

can i use forward instead of redirect for efficiency?

I have read the difference between forward and redirect.

http://grails.asia/grails-redirect-vs-forward

Sorry for the naive question but it seemed to me that since redirect goes back to browser and browser issues the new request, forward seems more efficient. So i was wondering why use redirect at all when the same purpose is achieve by using forward which is more efficient? Are there situations when the right thing to use is redirect instead of forward. Thanks for help!

The main difference here is the browser getting "touched" in redirect (mentioned already).

It's easy to demonstrate it for the login use-case.

A user gets a /login page and submits a login form to /doLogin , that results in his /profile page being shown. The login action validates the user input.

Here comes the critical difference: if the action performs forward operation, than the user is going to see /doLogin in the url-box. That's why the action should perform redirect , so that the user sees /profile in the url-box AND so the browser can process back-navigation properly.

For other actions which do not require any state change which should be considered in browser history, you could use forward .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM