I need a help on preventing form resubmit. My application uses struts framework and hybernate as business layer. Problem is when user fills up form and clicks submit button more that once or refreshs page while previous request is in process. Duplicate data get submitted. pls tell me the solution for above.
The use of Struts tokens did not work for me for a couple reasons. The first is that my application has popup windows with forms (say you are looking at a user record and you click a button to edit a phone number and you can edit the phone number in popup window). The problem that this caused for me is that if I enable tokens on the popup window then it clears the tokens needed for the parent page.
The bigger issue that I had is figuring out what to do when the code detects a duplicate submission. When you submit a form several things could happen. Maybe based on the entered fields the user it taken to another page in the wizard, or maybe the page is redisplayed because of validation errors, or maybe the submission failed because of a database error. The problem is that when the user clicks the second submission the start a new request and the user will never see the results of the first submission. About all you can do is show a page with a message like "You already clicked the 'Place Order' button...maybe the order was placed...maybe the item was out of stock...maybe your credit card was not valid...I don't really know".
Well , A solution to the BACK Button and then again clicking can be handled as pointed by DOM i.e by Using the Synchronizer Design Pattern which has methods like SaveToken, IsTokenValid etc... I have not practically done it but it can be done. Please refer to the Link given by Merrill in Struts FAQ and try it.
Apart from that , About Refreshing the Page . It is very simple to Handle.
Lets say , You have a action called Submit.do in which all processing is done and then finally there is a forward like this
and in struts-config , You have an entry like this :
Well , The forward element's redirect="true" sends the current response to the actual JSP and changes the url in the Address bar , So now when you submit it , The URL in the address bar would look like this :