Three situations and solutions for repeated form submission, and three forms

Source: Internet
Author: User

Three situations and solutions for repeated form submission, and three forms

First case: After the form is submitted, refresh the page without any other operations. The form will be submitted multiple times.

-Write an output in the servlet to determine whether to submit multiple times.

System. out. println ("inserted"); request. getRequestDispatcher ("/login_success.jsp"). forward (request, response );

-In this case, the number of "inserted" will be displayed in the Controller for the number of refresh times ".

 

-Root Cause: After the Servlet finishes processing the request, it forwards it directly to the target page, so that the entire service only sends one request, when you click Refresh in the browser, the previous request will be refreshed all the time.

-Solution: you do not need to forward data to another page and redirect data to the target page.

response.sendRedirect("/day0815-session/login_success.jsp");

 

Case 2: When submitting a form, if the network speed is poor, You may click the submit button multiple times. In this case, the form will be submitted repeatedly.

-Solution: Click the submit button to make the button unavailable. Complete with js

<Script type = "text/javascript"> window. onload = function () {// get the button object var btn = document. getElementById ("btn"); // bind to the button and click the response function btn. onclick = function () {// click it to make the button unavailable this. disabled = true; // when the submit button is set to unavailable, the default behavior of the button is automatically canceled. // manually submit the form this. parentNode. submit () ;};}; </script>

<Form action = "$ {pageContext. request. contextPath}/SessionServlet">
User: <input type = "text" name = "username">
Password <input type = "password" name = "pwd">
<Input type = "submit" value = "submit" id = "btn">
</Form>

-Note that the "Submit" button is unavailable!

 

Case 3: After the form is successfully submitted, click the back button in the browser, and click Submit to submit the form again without refreshing the page.

-Root Cause: when the server processes the request, it does not check whether the request is submitted repeatedly.

-Solution:
Use a token mechanism
-Token indicates the token.
-The server checks the browser's token before processing the request.
-The token is created by the server and handed over to the browser. The browser must carry this token when sending a request to the server.
-Check whether the token is correct before the server processes the request. If yes, the request is processed normally. Otherwise, an error page is returned.
-The token created by the server can be used only once.
-The token generally uses a unique identifier.

-On the jsp page, obtain the uuid as the token.

-UUID: A 32-Bit String, usually used as the unique identifier of an object or table. It is generated based on the machine code and timestamp (from January 1, January 1, 1970 to the present.

<% String uuid = UUID. randomUUID (). toString (); session. setAttribute ("uuid", uuid); %>
$ {Errormsg}
<Form action = "$ {pageContext. request. contextPath}/SessionServlet">
<Input type = "text" name = "uuid" value = "<% = uuid %>"/>
User: <input type = "text" name = "username">
Password <input type = "password" name = "pwd">
<Input type = "submit" value = "submit" ">
</Form>

-On the servlet page

String reqUUID = request. getParameter ("uuid"); HttpSession session = request. getSession (); String sessUUID = (String) session. getAttribute ("uuid"); session. removeAttribute ("uuid"); if (reqUUID. equals (sessUUID) {response. sendRedirect (request. getContextPath () + "/login_success.jsp"); System. out. println ("inserted");} else {request. setAttribute ("errormsg", "repeated login"); request. getRequestDispatcher ("/3.jsp "). forward (request, response );}


-Dangers of repeated form submission:
-Insert a large amount of duplicate and meaningless data into the database, occupying server resources
-The server that processes the request does not check whether the request is a duplicate request, resulting in malicious attacks.

 

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.