1

I was reading a web page guide about Java web development best practices here and I came across a section that gave advice about how to design servlet behaviour in the controller layer.

It is mentioned that if the request made to the servlet would involve an edit to the data store, a redirect must be used, otherwise a forward to another url should be used.

While I understand the differences between redirects and forward, I can't figure out how that is relevant in this case. In other words, I don't see how a redirect as opposed to a forward would prevent non idempotent datastore operations from being unnecessarily repeated by duplicate browser requests.

Sure, redirecting to a view page after a request to a controller JSP would change the URL but it would do nothing to stop deliberate attempts to access the same page again.

If protection against faulty or unnecessary database edits is required, my approach would be to check for that in the controller layer and redirect to an appropriate error page if necessary.

Please advise.

6
  • Using redirects prevents POST requests from the browser from being duplicated by simply refreshing or hitting the back button, and gives the user bookmarkable page landings.
    – GriffeyDog
    Apr 21, 2016 at 14:35
  • If you use a redirect then the original edit request is lost as your browser will only retain the url you will be redirected to. If forward, then all the navigation path will be in the history. Apr 21, 2016 at 14:36
  • You already gave the answer yourself: idempotence (bookmarkability). How exactly do you want a web page to behave when you bookmark/copypaste/share the current URL in browser's address bar into an entirely different window/session/application? This has nothing to do with double submits on page refresh.
    – BalusC
    Apr 21, 2016 at 14:41
  • correct, but simply redirecting to new page wont stop user from entering the URL in their address bar and accessing it again. I know most users are not malicious and will not deliberately look at the source and save the link of the "submit" button but it is a possibility Apr 21, 2016 at 14:41
  • 1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.