Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways to prevent duplicate submissions

Shulou Source: shulou.com Published: 2022-06-03 06:48:22 09月11日 Update

This article focuses on "what are the ways to prevent duplicate submissions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the ways to prevent repeated submissions.

1 javascript, set a variable to allow submission only once

Var checkSubmitFlg = false; function checkSubmit () {if (checkSubmitFlg = = true) {return false;} checkSubmitFlg = true; return true;} _ document.ondblclick = function docondblclick () {window.event.returnValue = false } _ document.onclick = function doconclick () {if (checkSubmitFlg) {window.event.returnValue = false;}}

2 or javascript, set the submit button or image to disable

3 using the synchronous token mechanism of struts

Synchronous token (Token) mechanism is used to solve the problem of repeated submission in Web applications, and Struts also gives a reference implementation. Basic principles:

Before processing the incoming request, the server compares the token value contained in the request with the token value saved in the current user session to see if it matches. After the request is processed, and before the reply is sent to the client, a new token is generated that, in addition to passing to the client, replaces the old token saved in the user's session. In this way, if the user goes back to the previous submission page and submits it again, the token passed by the client is inconsistent with the token on the server side, thus effectively preventing the occurrence of repeated submission.

If (isTokenValid (request, true)) {/ / your code here return mapping.findForward ("success");} else {saveToken (request); return mapping.findForward ("submitagain");} Struts generates a unique (for each session) token based on the user session ID and the current system time, which can be implemented by referring to the generateToken () method in the TokenProcessor class. 1. Verifying the transaction control token automatically generates an implied input representative token based on the identity in the session, preventing it from being submitted twice. In action: if (! isTokenValid (request)) errors.add (ActionErrors.GLOBAL_ERROR, new ActionError ("error.transaction.token")); resetToken (request); / / delete tokens in session 3. Action has such a method to generate tokens protected String generateToken (HttpServletRequest request) {HttpSession session = request.getSession () Try {byte id [] = session.getId (). GetBytes (); byte now [] = new Long (System.currentTimeMillis ()). ToString (). GetBytes (); MessageDigest md = MessageDigest.getInstance ("MD5"); md.update (id); bmd.update (now); return (toHex (md.digest () } catch (IllegalStateException e) {return (null);} catch (NoSuchAlgorithmException e) {return (null);}} so far, I believe you have a deeper understanding of "what are the methods to prevent duplicate submissions". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Tokens methods clients clients users generation content server mechanism reference synchronization processing learning service practical deeper valid consistent transaction representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Huawei Xiaomi MariaDB