aspose file tools*
The moose likes Struts and the fly likes Form Resubmitting problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Form Resubmitting problem." Watch "Form Resubmitting problem." New topic
Author

Form Resubmitting problem.

abhijit salvithal
Greenhorn

Joined: May 19, 2005
Posts: 7
Hi,

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.

Abhijit.
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
Read up on Tokens (isToken(), saveToken(), resetToken())
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This question is answered in the JavaRanch Struts FAQ


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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".

There is an article on the web somewhere that has a solution to this problem (error and forward values were stored on the session) but it seem more complex than I wanted to deal with. Since my application requires JavaScript to be enabled, I went with a client-side browser solution. You can search around for various implementations (and I am sure they all have limitations). My solution involved adding a method to my common JavaScript library and calling that function in the onsubmit handler of the form tag. One downside is that this requires adding code to a bunch of JSP files.

Here is the code:


- Brent
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
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

mapping.findForward("success")

and in struts-config , You have an entry like this :

<action path="/Submit"
name="FORMNAME"
validate="false"
scope="request">
<forward name="success" path="/login/SuccessInsert.jsp">
<action>

For the forward element of the action tag , Add an entry called redirect="true" so that it looks like this :

<forward name="success" path="/login/SuccessInsert.jsp" redirect="true">

What are we doing here ??

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 :

http://localhost:8080/login/SuccessInsert.jsp

Now , even if the user refreshes the page , It is the JSP file which would get refreshed and not your action.

Hope i am clear.

In case you did not understand the above explanation , please write to me back.

Regards ,

Yogendra N Joshi


Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
abhijit salvithal
Greenhorn

Joined: May 19, 2005
Posts: 7
Thank you every body,
thanks Dom Lassy,

I thinks i should checkout struts tokens.

Thanks & Regards,
Abhijit
 
 
subject: Form Resubmitting problem.