To solve the problem of repeated form submission in projects, there are the following situations in ordinary projects where repeated form submission may occur, such as:
1. Due to the slow server or network delay, click the Submit button repeatedly
2. The submission has been successfully completed, but the successful page has been refreshed
3. The submission has been successfully completed. By falling back, click the Submit button again.
All of these situations may cause too much of the same redundant data to be generated in the database and waste database resources. Only forwarding will appear, and redirecting will not.
The solution for the first case (using JavaScript) does not work for the latter two:
First add JavaScript code in the following format to the page
var submitFlag=false; function checksubmit(){ if(!submitFlag){ submitFlag=true; document.forms[0].submit(); } }(1) When the type attribute of the submit button is button:
Just set the button onClick=”checksubmit();”.
(2) When the type attribute of the submit button is submitted:
Set the button's onClick="checksubmit();" and add onsubmit="returnfalse" to the <form> tag. Because when the submit button is automatically submitted, after the click event is submitted, the onsubmit is set to "returnfalse", the automatic submission of the submit button will be invalid.
For the following two situations, you can use session to solve the problem. The principle is to place information in the session when running the information adding page, and then submit the information to the servlet for processing on the page. The servlet obtains the information in the session. If there is information in the session that is not empty, insert the information into the database, and then delete the information in the session. The information in the session is empty the next time the form is submitted, then the stored information process will not be executed.
However, simply using session has its limitations, so in actual development, it is generally used to use session combined with UUID to solve repeated submissions of forms. The code is as follows:
Define a UuidToken class
public class UuidToken {private static UuidToken UuidToken=new UuidToken();private UuidToken(){}public static UuidToken getUuidToken() {return UuidToken;}public synchronized String getUUIDAsStr(HttpServletRequest request){HttpSession session=request.getSession();String uuidStr=UUID.randomUUID().toString();if(uuidStr!=null){session.setAttribute("session.uuid", uuidStr);}return uuidStr;}/** * Determine whether the uuid saved in the session and the uuid on the jsp page are equal*/public synchronized Boolean isUUIDValidate(HttpServletRequest request) {//Get the existing session HttpSession session=request.getSession(false);if(session==null){return false;}String sessionuuid = (String)session.getAttribute ("session.uuid");if(sessionuuid==null){return false;}String htmlluuid=request.getParameter("html.uuid");if(htmlluuid==null){return false;}return sessionuuid.equals(htmlluuid);}/** * Delete uuid */public synchronized void resetUUID(HttpServletRequest request) {HttpSession session=request.getSession(false);if(session==null){return;}session.removeAttribute("session.uuid");}}Add a hidden domain to the jsp page, use uuid to generate a unique identification number, assign it to the hidden domain, and place the unique identification number into a copy of the session. The code is as follows:
<input type="hidden" name="html.uuid" value='<%=UuidToken.getUuidToken().getUUIDAsStr(request)%>'>
After submitting to the servlet, get the value of the hidden domain of the jsp page, and compare the value of the hidden domain with the value placed in the session. If the same, save the data and delete the UUID from the session. If it is not the same, it means that it is a repeated submission and no processing is performed.
Boolean flag= UuidToken.getUuidToken().isUUIDValidate(request);if(flag){// Save information to the database//Delete UuidToken.getUuidToken().resetUUID(request);} else{System.out.println("Form Repeated Submission");}Summarize
The above is all about this article's brief discussion on using Session to prevent repeated submissions of forms. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!