JSP token prevents forms from repeating submissions

Source: Internet
Author: User

Principle:

Servlet page Code:

1. Each request produces a token (generally timestamp), stored in the session and followed by hidden submission, in the servlet to determine whether the received token and session consistency to determine whether to repeat the submission, if not the recurrence of a Token is stored in the session to overwrite the original token.

2. When the user returns or refreshes the repeated request servlet, the servlet determines whether the token is consistent and, because the requester does not produce a new token, is inconsistent with the new token of the servlet and considers the duplicate submission.

3. When the user in the Request page refresh is again in the request page generated token, when the new token overlay servlet generated token, then token consistent, considered a new request.

JSP page code

<body>
    <%
           long Token=system.currenttimemillis ();    Token
            Session.setattribute ("token", token) producing time stamps;   
          
    %>
    <form  action= "isrepeat" method= "POST" >
        <input type= "text"  name= "username"/>
        <input type= "text"  name= "password"/> <input type= "hidden" value= "<%=token%>" Name= "
        Token "/>   <!--submitted as hidden-->
        <input type=" Submit "value=" submitted "/>
    </form>
</body>

Interface code:

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)

            throws Servletexception, IOException {
         req.setcharacterencoding ("Utf-8");
         Resp.setcharacterencoding ("Utf-8");
         Resp.setcontenttype ("Text/html,charset=utf-8");
         String Username=req.getparameter ("username");
         String password=req.getparameter ("password");
         Long Token=long.parselong (Req.getparameter ("token"));
         Long Tokeninsession=long.parselong (Req.getsession (). getattribute ("token") + "");
         if (token==tokeninsession) {
            resp.getwriter (). println ("OK");
                        If this is the first request, a new token
                        req.getsession () setattribute ("token", System.currenttimemillis ())
         is generated. else
         {
            
            resp.getwriter (). println ("Do not repeat submit");
         }
    


Related Article

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.