wood burning stoves 2.0*
The moose likes Servlets and the fly likes form submission Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "form submission" Watch "form submission" New topic

form submission

Sujit Nene

Joined: Apr 02, 2001
Posts: 6
I have a problem when submitting a form. Say, I submit a form to a servlet from a jsp page. From servlet I am forwarding the request to another page. after getting the forwarded page, if i refresh that page, the form on the previous page is again gets submitted. This results into an erroneous record insertion into databse. How can i avoid this scenario?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Sujit Nene:
[Refreshing the page displayed after form submission] This results into an erroneous record insertion into databse. How can i avoid this scenario?

You could change the forward into a redirect, I guess. Or you can do what I've seen some sites do, warn your users not to refresh the page.
But those are all bad solutions. What you need to do is detect duplicates and prevent them from entering the database. This is really easy - for instance, you could stick a timestamp (System.currentTimeMillis()) in a hidden field of your form, store it in your database record, and for good measure impose a uniqueness constraint on (user, timestamp) at the database level even if you detect duplicates in your application. That is your absolute guarantee that duplicate submissions will never make it into the database.
- Peter
I agree. Here's the link: http://aspose.com/file-tools
subject: form submission
Similar Threads
Setting session bean values from java classes.
Auto-submit <form> to a servlet.
Question on using jQuery's .attr AFTER form is submitted
bean not populated
Submit the form