简体   繁体   中英

sendRedirect VS requestDispatcher.forward for error page?

So there is an error.jsp page and I want user to occur on this page after he/she inputs invalid data. Is it better to use sendRedirect or requestDispatcher.forward for redirecting to the error page?

I know, it may depend on whether I want to put some attributes to the request object and so on. And I'd prefer to use requestDispatcher.forward , but is it safe? Cause in this case after page refreshing the data will be sent to the server one more time. Yeah, it doesn't make any effect (insert, update, delete) on database since the data is invalid so the user occurs at the error page once again. But... idk, I feel like something is being wrong with the approach of using requestDispatcher.forward , even though it's kind of idempotent action.

So what do you think?

The important difference is :

The function sendRedirect will send a 302 response code to browser, then the browser will send another request to server again

https://en.wikipedia.org/wiki/HTTP_302

but the function requestDispatcher.forward is a server internal redirection, The browser has no feeling about it !

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