Dark Horse DAY05 session to prevent duplicate submissions & small Cases

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.