aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Disable Button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Disable Button" Watch "Disable Button" New topic
Author

Disable Button

Steve Watson
Ranch Hand

Joined: Apr 08, 2003
Posts: 38
I have a form on a JSP that sometimes takes a few seconds to save data to the database. If the user clicks the button a second time bad things can happen. How can I disable the button after it is first clicked until the form action is complete?

Code snippet:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Your web app should be written to handle such situations, but it's also a good idea to disallow the user from getting themselves into trouble in the first place.

To disable a button simply set its disabled property to true.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Esteban Puertas
Greenhorn

Joined: Mar 23, 2008
Posts: 22
Maybe you can consider using the post/redirect/get pattern instead of trying to disable the user button.

Post/Redirect/Get
Redirect after post

This pattern prevents the user from sending more than one time the form data.

Hope this helps.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It looks like you're using Struts which, I believe, has a built in mechanism for implementing the Synchronizer Token pattern which is also a way to avoid the double submit problem.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Originally posted by Esteban Puertas:
Maybe you can consider using the post/redirect/get pattern instead of trying to disable the user button.
PRG helps mostly with double posts due to the refresh button rather than double-clicking.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Disable Button