On using session to prevent repeated submission of forms

To solve the problem of repeated submission of forms in projects, the following situations may occur in ordinary projects, such as:

1. Click the submit button repeatedly due to slow server or network delay

2. It has been submitted successfully, but the success page has not been refreshed

3. It has been submitted successfully. Click the submit button again by going back.

These situations may cause too much same redundant data in the database and waste database resources. Only forwarding occurs, and redirection does not.

The solution for the first case (using JavaScript) has no effect on the latter two:

First, add JavaScript code in the following format to the page

(1) When the type attribute of the submit button is button:

Onclick = "checksubmit();" setting button Just.

(2) When the type attribute of the submit button is submit:

Onclick = "checksubmit();" of the setting button, And add onsubmit = "returnfalse" in the < form > tag. For the automatic submission of the submit button, after clicking the event submission, set onsubmit to "returnfalse", and the automatic submission of the submit button will become invalid.

For the latter two cases, you can use session to solve them. The principle is to place information in the session when running the add information page, and then the page submits the information to the servlet for processing. The servlet obtains the information in the session. If the information in the session is not empty, insert the information into the database, and then delete the information in the session, If the information in the session is empty the next time the form is submitted, the stored information procedure will not be executed.

However, the simple use of session has its limitations. Therefore, in actual development, it is generally used to solve the repeated submission of forms by using session combined with UUID. The code is as follows:

Define a uuidtoken class

Add a hidden field on the JSP page, use UUID to generate a unique identification number, assign it to the hidden field, and place the unique identification number in the session. The code is as follows:

After submitting to the servlet, obtain the value of the hidden field of the JSP page, and compare the value of the hidden field with the value placed in the session. If it is the same, save the data and delete the UUID from the session. If it is different, it indicates that it is repeated submission and no processing.

summary

The above is all about using session to prevent repeated submission of forms. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>