Sometimes in order to prevent users from repeating the submission we only need to write the foreground code, a JavaScript will be done, but also may be bad people deliberately modify the source code, so that prevents duplicate submissions, so we implement in the background to prevent duplicate submissions.
1.login.jsp Front desk java| Script implementation to prevent repeated submissions, in order to implement the background so set a random number to the session field, and then submit the form when the time to hide a random number. To achieve Subservlet acquisition.
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >2.SubServet Implementation of the background to prevent duplicate submissions, if the user is the first time to submit to determine the random number from the session and the hidden obtained from the form of random number is the same, if the same, the session in the domain object is emptied, Then output the corresponding data to the browser, if not the same description is repeated, this time the session in the domain object is already null, so two values are not equal. Remind users that they cannot repeat the submission. Package Cn.itheima.resubmit;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Resubservlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException { Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); String username = request.getparameter ("username"); try {thread.sleep (1000*4);} catch (Interruptedexception e) {throw New RuntimeException (e);} String r1 = request.getparameter ("Randomnum"); string r2 = (string) request.getsession (). getattribute ("Randomnum"); if (r2!=null&&! "". Equals (r2) &&r1.equals (R2)) {request.getsession (). RemoveAttribute ("Randomnum");// After the first commit, remove the object from the session response.getwriter (). Write ("name is:" +username);} Else{response.getwriter (). Write ("Please do not repeat commits!"). Ok?! ");}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doget (request, Response);}} Operation Result:If you repeat the submission: that is, always click the Submit button ..... Reception and the day After tomorrow tips:
Dark Horse DAY05 session to prevent duplicate submissions & small Cases