File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how can I stop the submit being processed twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how can I stop the submit being processed twice?" Watch "how can I stop the submit being processed twice?" New topic
Author

how can I stop the submit being processed twice?

James Byars
Ranch Hand

Joined: Apr 15, 2009
Posts: 42
hi,

I have an application
user clicks submit
data sent to database
user redirected to another page.

The problem is, it takes about 5 seconds from the time the user clicks submit to the time they're redirected to the other page. And in this 5 seconds, users sometimes double click on Submit, so the data is submitted twice! I want my servlet to only accept one submit within say 5 seconds in a Session. How can I do this?

thank you
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

You need to design your transactions to be idempotent.
http://en.wikipedia.org/wiki/Idempotence

The short answer is that you can not stop it, so you have to handle it in your design.
James Byars
Ranch Hand

Joined: Apr 15, 2009
Posts: 42
thanks, I was hoping more along the lines of someone telling me that there is some design pattern or method already available that I can make use of
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

Idempotency is the design pattern. For some actions, its trivial. You simply return the same result as you would have returned the first time it was executed. So if the command is "delete record for James Byars" you return "all records for James Byars deleted"

If it was "create James Byars record" then you have problems. So change the definition to "make sure that there is a James Byars record" and then its easy.

The hard one is "add one to James Byars's balance"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Read this article and pay particular attention to the PRG pattern.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Charbel Keyrouz
Ranch Hand

Joined: Jun 10, 2005
Posts: 46
You can simply disable the button submit after the first click.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Doing something in the client is not a guarantee. Have a read of the article Bear linked; it’s a far more thorough solution.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
James Byars
Ranch Hand

Joined: Apr 15, 2009
Posts: 42
thanks a lot guys, I will definitely look into that article later on when I develop larger web applications, for the time being I will just use some client side disabling I guess
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

James Byars wrote: I will just use some client side disabling I guess

Seriously, don't do that. It breaks the user's expectations and does not solve your problem.

James Byars
Ranch Hand

Joined: Apr 15, 2009
Posts: 42
Pat Farrell wrote:
James Byars wrote: I will just use some client side disabling I guess

Seriously, don't do that. It breaks the user's expectations and does not solve your problem.



ok, I won't! I'll find the time to read up on the earlier link and work through it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Pat Farrell wrote:
James Byars wrote: I will just use some client side disabling I guess

Seriously, don't do that. It breaks the user's expectations and does not solve your problem.

Quoted for truth. Solutions that don't solve the problem aren't solutions at all.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
I was wondering if you could simply display a message saying that the page is being loaded please wait... or something of this sort.

When I go internet shopping, I see such messages being displayed at many payment gateways. They ask the user to submit only once as clicking the button twice would result in duplicate transactions.

One more suggestion - not sure if it would be a good idea, but I think that instead of disabling the submit button, you can add a Load Mask on the entire page while the redirect takes place. Thus the user won't be confused and would wait patiently for the redirect. Just an idea...


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can I stop the submit being processed twice?