1. Javascript. Set a variable and submit it only once. <Script language = "JavaScript"> VaR checksubmitflg = false; Function checksubmit (){ If (checksubmitflg = true ){ Return false; } Checksubmitflg = true; Return true; } Document. ondblclick = function docondblclick (){ Window. event. returnvalue = false; } Document. onclick = function doconclick (){ If (checksubmitflg ){ Window. event. returnvalue = false; } } </SCRIPT> <HTML: Form Action = "myaction. Do" method = "Post" Onsubmit = "Return checksubmit ();"> 2 or Javascript. Set the submit button or image to disable. <HTML: Form Action = "myaction. Do" method = "Post" Onsubmit = "getelbyid ('submitinput'). Disabled = true; Return True; "> <HTML: Image styleid = "submitinput" src = "images/OK _ B .gif" Border = "0"/> </Html: Form> 3. Use the struts synchronization token mechanism The synchronization token mechanism is used to solve the problem of repeated submission in Web applications. Struts also provides a reference implementation. Basic Principles: Before processing the incoming request, the server compares the token value contained in the request with the token value saved in the current user session to see if it matches. After the request is processed and the reply is sent to the client, a new token will be generated. In addition to sending the token to the client, the old token saved in the user session is also replaced. In this way, if the user goes back to the submission page and submits the request again, the token sent from the client is inconsistent with the token sent from the server, effectively preventing repeated submission. If (istokenvalid (request, true )){ // Your code here Return Mapping. findforward ("success "); } Else { Savetoken (request ); Return Mapping. findforward ("submitagain "); } Struts generates a unique (for each session) token based on the user session ID and the current system time. For details, refer to the generatetoken () method in the tokenprocessor class. 1. // verify the transaction control token. <HTML: Form > An implicit input token is automatically generated based on the identifier in the session to prevent two submissions. 2. In Action: // <Input type = "hidden" Name = "org.apache.struts.taglib.html. Token" // Value = "6aa35341f25184fd996c4c918255c3ae"> If (! Istokenvalid (request )) Errors. Add (actionerrors. global_error, New actionerror ("error. transaction. Token ")); Resettoken (request); // Delete the token in the session 3. Action has such a method to generate a token Protected string generatetoken (httpservletrequest request ){ Httpsession session = request. getsession (); Try { Byte ID [] = session. GETID (). getbytes (); Byte now [] = New Long (system. Cu Rrenttimemillis (). tostring (). getbytes (); Messagedigest MD = messagedigest. getinstance ("MD5 "); Md. Update (ID ); Md. Update (now ); Return (tohex (Md. Digest ())); } Catch (illegalstateexception e ){ Return (null ); } Catch (nosuchalgorithmexception e ){ Return (null ); } }
|