Thursday, July 24, 2008

Problems of Duplicate / Multiple Form Submissions - Solutions

How to avoid duplicate form submissions?

It is a common problem in many applications where the duplicate submission occurs. This would happen in the following circumstances:

1. When the user clicks the ‘Submit’ button quickly twice without patience.
2. When the user cliks on ‘Back’ button of the browser and clicks on ‘Submit’ again.
3. When the user refreshes the form page again which submits the form.

These problems can be avoided by the special feature provided by struts called token. Let us see clearly about token feature in struts.

What is the concept behind struts’s token feature?

Everytime the page with the form is shown to the user, internally there is a hidden form field with the property name ‘TOKEN’ which has very long random string value. Whenever the form page is displayed, the token is created with the new value. When we use ‘Back’ button, unfortunately, the TOKEN value is the one which has been already submitted and verified by Struts during the previuos action call. So during the duplicate submission, struts declares it as an invalid token and hence the warning or error is raised by throwing suitable exceptions.

How do we implement this token feature?

It is very simple and very little programming is required related to this. Struts provides three methods for this purpose, which can be used in Action classes. They are:

a. saveToken(request)
b. isTokenValid(request)
c. resetToken(request)


Step 1: Create a dummy action class prior to come to the specific form page which calls the method saveToken(request). This process initializes a new token in request. Forward this to the JSP page where the form is found (where the value is entered).

Step 2: Form is submitted and action class is called. (In this page, the hidden form field is automatically created).

Step 3. Inside your action, when you call INSERT or UPDATE call the method ‘isTokenValid’ which returns boolean value and you can hence decide if it is a valid submission or duplicate submission, such as if (!isTokenValid(request)) throw ….

Step 4: After the successful INSERT or UPDATE, we should not forget to reset the token value in session. The token can be reset by resetToken(request)

Hope this conept is useful to you. There are plenty of other real-time scenarios and problems that we come across day by day. We shall discuss all such design issues in blogs to come.

Till then, See you.

Regards,

Swathika.

For queries, write to me to swathikalakshmi@gmail.com

4 comments:

Lucy said...

Similar to these, javapapers.com has good quality of java faq. Check it at you leisure!

Sarav said...

This is really very useful info which
you have provided. I'm using latest version of struts 2.0.How we can solve this duplicate submit issue.
whenever i refresh the page the action is executed twice.

lokesh kumar said...

Hi Siva,

This is very useful.... But same Problem we have to restrict by using Struts2 Tokenizer concept... If you have please share in your blogger..

Nayan Bijagare said...

What is the initial value of token when 1st time form submitted?