• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Form Resubmitting problem.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read up on Tokens (isToken(), saveToken(), resetToken())
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is answered in the JavaRanch Struts FAQ
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
abhijit salvithal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every body,
thanks Dom Lassy,

I thinks i should checkout struts tokens.

Thanks & Regards,
Abhijit
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic