Reputation: 207
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?
Upvotes: 0
Views: 690
Reputation: 964
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 !
Upvotes: 2