aspose file tools*
The moose likes Servlets and the fly likes How to prevent to click multi submit button? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to prevent to click multi submit button?" Watch "How to prevent to click multi submit button?" New topic
Author

How to prevent to click multi submit button?

qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
In my jsp page,I have two submit button,they will jump to different page,but when I first clicked one button then clicked the second button as quickly,my servlet cannt do with it,I want to forward to the page when the one's first click,ignore the second click.How can I do it?
Thanx alot.


Thanks...qingwu<br />When I open my eyes,I see your pretty face.
Srinivas Jag
Greenhorn

Joined: Jun 30, 2003
Posts: 9
Are you using Javascript to define 'submitXXX' functions for the click of the two buttons?
If so, you can try defining a flag as a global javascript variable.
This flag would be set when you click either of the buttons the first time, and submit would take place based on the state of the flag.
Hope this helps.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
An alternative would be a piece of Javascript that runs in the onclick event of the button and disables the buttons before submitting the form.
That way they cannot be clicked again.


42
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
One way to do this without using javascript:
before you reach your page with the two buttons, generate a token (it can be a random number), store it in a session, and place it as a hidden value in your form.
When the user clicks the first button, get the value from the session and compare it with the hidden value. If they're the same, it means it's the first button that it was clicked. remove the token from the session.
If the user gets to click the second button, your code will check again the hidden value against the one in the session. But, since you've removed the token from the session already, the values won't be the same. You can discard this request, since you know it was the second click and not the first.
easy
[ September 09, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
EJ Dinglasan
Ranch Hand

Joined: Jun 08, 2003
Posts: 47
Hi!
IMHO
I think that disabling the buttons before submitting is better so that the user won't be confused if which action was really submitted.
=)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Since you can never rely on client-side Javascript executing, Andres solution is actually the more robust one.
In practice, I may frequently perform client-side actions to enhance the 'user experience' -- this include form data validation -- but my server-side code never assumes that any client-side code has executed.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
Thanks,It's nice of you!
Because my browser cann't support javascript so much,I will take Andres' method,How can I discard request?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by qingwu wang:
Thanks,It's nice of you!
Because my browser cann't support javascript so much,I will take Andres' method,How can I discard request?

It depends on your requirements. It's up to you on what you're gonna do once you get the unwanted request. I implemented this technique when a user was filling out his details. In this way, my data was not saved in the database twice.
If everything went ok, I forwarded the request to a thank you page. So, if the user clicked the button twice, I immediately forwarded it to thankyou.jsp, without processing anything.
hope it helps
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
QTN:
My process as following
servlet(get serveral string)->jsp(ervery string generate one submit)->servlet(according to submit attribute jump to different page)->to servral different page.then the button in the jsp can't allow to click more than one time before jumping other page.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Can't you just do something like:

and that's it. if the second request comes in, it'll do nothing, coz A not equal to B (It doesn't go inside the if statement). You know that this request should be discarded, since you've already redirected it to your jsp.
does that make sense?
[ September 09, 2003: Message edited by: Andres Gonzalez ]
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
To:Andres

the above snippet code,when I clicked two submit button,hs.getAttribute("flag")==null ?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
disclaimer: dirty code

In the servlet
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
Thanks Andres,thanks a lot!!!
I will try it!
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
hello everyone!
I found a piece of js code as following,but I click the submit button,it seems no changes.I think it should change grey.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
I want to remove the form after submit on time.How can I do it?
if I delete submit button after submit?
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
I think making the visibilty of the other submit button to false after u click one button would also be a good choice...
Cheers,
Gaya3
-------------------------------------------
Life is a process of continuous learning...
If you are not learning .. You are decaying...
 
jQuery in Action, 2nd edition
 
subject: How to prevent to click multi submit button?