Solving the Double Submission Issue

后端 未结 7 1023
暖寄归人
暖寄归人 2020-12-01 03:06

I would like to see how Web Developers avoid the double submission problem. So basically my understanding of the problem is as follows:

Double submission occurs whe

7条回答
  •  [愿得一人]
    2020-12-01 03:22

    Using struts web-application framework we can handle this problem as follows:

    Struts has 3 methods use for the token, saveToken(), isTokenValid() and resetToken().

    saveToken() - generate the token key and save to request/session attribute.
    isTokenValid() - validate submitted token key against the 1 store in request/session.
    resetToken() - reset the token key.

    How it works:
    1) Upon loading the form, invokes saveToken() on the action class to create and store the token key. Struts will store the generated key in request/session. If the token successfully created, when view source on the browser you will see something similar to the following, the token key is stored as a hidden field:

    2) Once the form submitted, invokes isTokenValid() on the action class, it will validate the submitted token key(hidden field) with the token key stored previously on request/session. If match, it will return true.

    public final ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        saveToken(request);
        if (!tokenIsValid(request)) {
            //forward to error page saying "your transaction is already being processed" 
        } else {
            //process action 
            //forward to jsp 
        }
        // Reset token after transaction success. 
        resetToken(request);
    }
    

    reference

提交回复
热议问题