Three scenarios for avoiding form duplication submission in Web applications

Source: Internet
Author: User
web| Repeat | The first two types of repetition are the use of JavaScript, followed by a reference implementation with struts:

   1, JavaScript, set a variable, only allowed to submit 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>


   2, or JavaScript, the submit button or image to the Disable

       
        
         
        


   3, the use of struts synchronization token mechanism

Using the synchronization token (Token) mechanism to solve the problem of repeated submissions in Web applications, struts also gives a reference implementation.

Basic principle:

The server side compares the token value contained in the request to the token value saved in the current user session before processing the incoming request.

See if it matches. After the request has been processed, and before the reply is sent to the client, a new token is generated, which, in addition to passing to the

Client, the old token that is saved in the user's session is also replaced. This way if the user returns to the previous submission page and again

If submitted, the token from the client is inconsistent with the server-side token, effectively preventing the recurrence of the commit.

       
        
         
        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's session ID and current system time, and the implementation can refer to the Generatetoken () method in the Tokenprocessor class.

1.//Verify the transaction control token, --> Automatically generates an implied input token from the session identifier 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);    Remove tokens from session
       
         


3. Action has such a method of generating tokens

       
         
          
        Protected String Generatetoken (HttpServletRequest request) {       HttpSession session =     request.getsession ();       Try    {           byte id[] =      session.getid (). GetBytes ();           byte now[] =               new Long (System.currenttimemillis ()).      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);       }   
       
         


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.