Spring / Spring7 Intermediate to Advanced Interview questions
How do you prevent duplicate form submissions caused by double-clicking a submit button in a Spring MVC application?
No single technique fully solves this on its own, so a layered approach works best. Client-side, disabling the submit button in the click handler stops most accidental double-clicks, but it's not reliable on its own since JavaScript can be disabled, slow, or bypassed by a fast enough second click before the handler runs.
form.addEventListener("submit", () => { submitButton.disabled = true; });
Server-side, the synchronizer token pattern embeds a one-time token in the rendered form, tied to the session; on submission, the server checks and immediately invalidates that token, so a second submission carrying the same (now-invalidated) token is rejected. The Post/Redirect/Get pattern helps with a related but different case - refreshing or navigating back after a successful submit - by turning the post-submit page into a GET, though it does nothing about two POSTs fired before either completes. For APIs and modern SPA-driven forms, an idempotency key generated once per logical submission and sent as a header, checked against a short-lived store like Redis, catches duplicates regardless of what triggered them - a double-click, a flaky retry, or a resubmitted request.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
