Search code examples
htmlformsjspsubmitpage-refresh

Preventing dupliate form submission.


I have a form in my jsp file of the eclipse.

the form gets values and inserts them into my database.

I entered the values and pressed the submit button, so the values were inserted into my database.

the problem is: if I refresh the page, the previous values are inserted into my database again, although the inputs are empty.

String name = request.getParameter("name");

so after the refreshing, the name gets the last value that was inserted.

how can I fix it?


Solution

  • You should use the POST method for HTTP requests that perform an action, and GET for those that only retrieve data. Respond to the POST request by redirecting to a resource that would be requested with GET. That way, the form won't get resubmitted.