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.
More Related questions...