Struts Tutorial Home

Struts Home

JSP Resources

Community

Site

Struts solution to Duplicate Form Submission (Token mechanism)

text zoom

The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back or when a client accesses a view by returning to a previously book marked page. The struts provide the saveToken() and isTokenValid() to check whether the request is already submitted or not.


 


The method isValidToken() return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it. Returns false under any of the following circumstances:

  • No session associated with this request

  • No transaction token saved in the session

  • No transaction token included as a request parameter

  • The included transaction token value does not match the transaction token in the user's session


Note:- If the Action class is changed as per the following code then it will validate against the duplicate form submission.


 


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception


{
        ActionMessages errors = new ActionMessages();
    if (!isTokenValid(request)) {
    errors.add("error.duplicaterequest", new ActionMessage("error.duplicaterequest"));
        }
        resetToken(request);
    if (!errors.isEmpty()) {
                saveErrors(request, errors);
                saveToken(request);
        return (mapping.getInputForward());
        }
    return mapping.findForward("success");
}


 


Output


 


Note:- The output changes as follows :-


                    

Copyright © 2014 VisualBuilder. All rights reserved