aspose file tools*
The moose likes JSP and the fly likes submitting the invoice twice when submitt button is clicked 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 » Java » JSP
Bookmark "submitting the invoice twice when submitt button is clicked" Watch "submitting the invoice twice when submitt button is clicked" New topic
Author

submitting the invoice twice when submitt button is clicked

sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi all,

i have a JSP where i have some buttons and specially i have submit button of type button when the user presses this submit button very rarely in production it is submitting twice but i tried to recreate the problem in my test server but unable to create,i dont why know it is happening only in production and it is rare the following is the way how we handled to disable the buttons when it is clicked



<span class="button-blue HideInPrint"><input type="button" name="sButton" value="Submit" onclick"javascript:continueAction('submit');" <%=buttonStatus%> /></span>
<input type="hidden" name="submitButton" value="" />


function continueAction(typeOfAction)
{

disableButtons();
//Populate the appropriate hidden field that will be passed to the servlet,
//depending on the type of action performed on the page
if (typeOfAction == "submit") {
document.billSumm.submitButton.value="Submitted";
} else if (typeOfAction == "save") {
document.billSumm.saveAsDraft2.value="Saved";
}
else if (typeOfAction == "cancel") {
document.billSumm.cancel.value="Cancelled";
}

//Invoke submission
document.billSumm.submit();

}

function disableButtons()
{

if (document.all || document.getElementById) {
for (i = 0; i < document.billSumm.length; i++) {
var tempobj = document.billSumm.elements[i];
if (tempobj.type.toLowerCase() == "button")
tempobj.disabled = true;

}
}
}

please advice when does this problem occur and how over come it.
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
How do you know for sure it is the application submitting twice and not the user?

Regards

Matt
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi Matt,

thanks for your response, when the user submits an invoice the application generates a unique reference number and puts the enitre data on a MQ Queue of concerned to that refernce number later which is submitted to MVS DB
we confirm that application is generating as it is putting the message twice in the MQ queue for the same Reference Number
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
You can not rely on users to do what they are told, so they may submit more than once.
You can not rely on javascript to prevent them submitting more than once.

However, you can rely on the server not processing duplicate requests if you plan for it. Have a search for 'Synchronizer Token pattern' and you'll find some good descriptions and solutions for the problem.
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi Darren,

its a good idea from you, but it is built feature of Struts and my application is not based on struts just we have servlets and jsp's and i cannot change the flow as it in maintainence so can you suggest how can i implement this Synchronizer Token pattern

thanks
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
This is not exactly what the pattern describes, but could work in your case.

In your control (servlet) code, generate a random String - this is your token. Add the token into the user session.


Within your form add a hidden field called token which contains the token from the session.


In your control (servlet) code that processes the form you should check the hidden parameter 'token' matches the 'token' attribute stored in the user session. If it does, then remove 'token' attribute from the user session and process the form. If 'token' does not match, or does not exist in the session, you know it is a duplicate request.
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi Darren,

Thanks for your reply.


have a nice week end
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi Darren,

servlet where iam creating a string randomly
Double d = new Double(Math.random());
String token = d.toString();
session.setAttribute("token", token);


in the JSP
<input type="hidden" name="token1" value="${sessionScope.token}" />

and in the control servlet which fires when the jsp is submitted
StringstrJspToken = request.getParameter("token1");
String strToken = (String) session.getAttribute("token");
if strToken.equals(strJspToken)

i have tested with System.out.println and found that iam not getting value from Jsp hidden variable please can u suggest is there any mistake in my code

thanks
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
If the HTML page contains a value for token1 and if the parameter name is correct in the processing servlet then I am guessing that

does not appear within
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
HI Darren

i tried by keeping input tag not within <form> </form> in the Jsp
then also iam not getting iam not understanding why it is not getting the value.

Thanks
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
From your descriptions it seems more likely a HTML error than anything else. Can you post a HTML version (view source -> copy from within IE) of the page that displays the form? Make sure you strip out anything confidential or private domain names first though!
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
HI Dareen,

i looked in to the apllication closely and got to know that the value which i have set in Session
With in JSP iam not getting the session String variable in to hidden variable at all
<input type="hidden" name="token1" value="token" /> is not working is there any other way

thanks for continous support
sreedhar
Greenhorn

Joined: May 02, 2006
Posts: 20
Hi Darren,
i got the problem and cleared
thanks for your solution
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: submitting the invoice twice when submitt button is clicked