Duplicate submission of forms: not complete once, Request form page--and then submit form process to complete data submission
root cause: do not complete once, Request form page, then submit form procedure.
Causes a recurring commit:
- Repeat the click submit button due to slow server or network latency.
- Successfully submitted, Refresh Success page (forward) (Request forward).
- Already submitted successfully, by fallback, click the Submit button again
- Note: Back up, refresh the form page, re-submit, at this time, not to repeat the submission, but to send a new request, under Firefox, repeatedly submitted to the same address is invalid operation.
Case:
1@WebServlet ("/trans")2 Public classTransferservletextendshttpservlet{3 Private Static Final LongSerialversionuid = 1L;4 5 protected voidService (HttpServletRequest req, HttpServletResponse resp)6 throwsservletexception, IOException {7Req.setcharacterencoding ("UTF-8");8Resp.setcontenttype ("Text/html;charset=utf-8");9PrintWriter out =Resp.getwriter ();TenString money = Req.getparameter ("Money"); One //Analog network latency via sleep A Try { -Thread.Sleep (3000); -}Catch(interruptedexception e) { the e.printstacktrace (); - } -SYSTEM.OUT.PRINTLN ("Transfer amount:" +Money ); -Out.print ("Transfer amount:" +Money ); + } -}
Under the crazy point, you will find that the JSP page will not change, but through the background of the printout can be seen, will continue to output, indicating that has been executing
Workaround:
Before committing the guarantee, you must first request the form interface, the same principle as the verification code.----Token mechanism
On the first request, when the request to the form interface, create a token, when the click on the transfer, send the request, with this token, sent to the next interface, in the servlet to verify the token, this token in the session of a copy, in the form of a copy, equal description of the form is correct, and destroy the tokens in this session.
The code in the JSP page
1<%2 //Creating Tokens3String token =Java.util.UUID.randomUUID (). toString ();4 //There is one part of the session, then make judgment5Session.setattribute ("Token_in_session", token);6%>7 89<form action= "/trans" method= "POST" >Ten<input type= "hidden" name= "token" value= "<%=token%>"/> OneTransfer amount: <input type= "text" name= "Money" min= "1" required/><br/> A<input type= "Submit" value= "Transfer"/> -</form>
The code in the Transferservlet
1@WebServlet ("/trans")2 Public classTransferservletextendshttpservlet{3 Private Static Final LongSerialversionuid = 1L;4 5 protected voidService (HttpServletRequest req, HttpServletResponse resp)6 throwsservletexception, IOException {7Req.setcharacterencoding ("UTF-8");8Resp.setcontenttype ("Text/html;charset=utf-8");9PrintWriter out =Resp.getwriter ();Ten //get the token value in the form OneString token = req.getparameter ("token"); A //gets the token value in the session -String sessiontoken = (string) req.getsession (). getattribute ("Token_in_session"); - //tokens in the session are easy to empty because tokens in the session need to be destroyed. the - if(Token.equals (Sessiontoken)) { - //Description Token same -Req.getsession (). RemoveAttribute ("Token_in_session"); +String money = Req.getparameter ("Money"); -SYSTEM.OUT.PRINTLN ("Transfer amount:" +Money ); +Out.print ("Transfer amount:" +Money ); A //finally destroy the token in the session at } - //If the token is a different description, it is repeated and cannot be submitted - } -}
And then, in order not to appear in the JSP file Java code, the creation of the token and jump to put in another servlet
1@WebServlet ("/transfer")2 Public classCopyoftransferservletextendshttpservlet{3 Private Static Final LongSerialversionuid = 1L;4 5 protected voidService (HttpServletRequest req, HttpServletResponse resp)6 throwsservletexception, IOException {7 //Create a token and jump to submit.jsp8String token =Uuid.randomuuid (). toString ();9 System.out.println (token);TenReq.getsession (). SetAttribute ("Token_in_session", token); OneReq.setattribute ("token", token); AReq.getrequestdispatcher ("/views/repeatsubmit/submit.jsp"). Forward (req, resp); - - } the}
This is what the JSP file looks like in this case
1 <H3>Transfer interface</H3>2 <formAction= "/trans"Method= "POST">3 <inputtype= "hidden"name= "token"value= "${token}"/>4Transfer amount:<inputtype= "text"name= "Money"min= "1"Required/><BR/>5 <inputtype= "Submit"value= "Transfer" />6 </form>
A lot cleaner than the above, more neat, but still feel bad, because if needed in other places, but also need to create tokens, check tokens, delete tokens, so extracted to do a tool class
Tokenutil.java
1 //the token tool class2 //Creating Tokens3 //Checksum token4 //Destroying tokens5 Public classTokenutil {6 Private Final StaticString token_in_session = "Token_in_session";7 Public Static voidSavatoken (httpservletrequest req) {8String token =Uuid.randomuuid (). toString ();9 System.out.println (token);Ten req.getsession (). SetAttribute (Token_in_session, TOKEN); OneReq.setattribute ("token", token); A } - - Public Static BooleanValidatetoken (httpservletrequest req, the String tokeninrequest) { - //gets the token value in the session -String Sessiontoken =(String) req.getsession (). getattribute ( - token_in_session); + if(Tokeninrequest.equals (Sessiontoken)) { - req.getsession (). removeattribute (token_in_session); + return true; A } at return false; - } -}
This is good, the user only need to call this tool class, no need to write a series of operations such as creating tokens
Avoid repeating form submissions in Java