index.jsp TokenServlet.java for form submission processing Servlet
1. Repeated submission situation:
① Submit the form to a Servlet, and The Servlet responds to a JSP (html) page byrequest forwarding. At this time, the address bar still retains the path of the Servelt. Click "Refresh" on the response page;
② Click the "Submit button" when the response page has not reached the browser;
③ In the page that has been submitted, click "Return", and then click "Submit";
2. If it is not a repeated submission: on the page where the form has been submitted, click "Return", then "refresh" the original form page, and then "Submit"
3. How to avoid repeated submission of the form:
Make a mark in the form, and when submitting it to the Servlet, check whether the mark exists and whether it is consistent with the predefined mark
If consistent, the request will be accepted and the mark will be destroyed ;
If it is inconsistent or there is no mark, respond directly to the prompt message: "Duplicate submission"
① Option 1: Only provide one hidden field: 5881c2e62d61ab914c6fe21cf4a191c1, not feasible, reason: //Clear mark: There is no way to clear fixed request parameters
index.jsp
1 2 <% 3 request.setAttribute("token", "tokenValue"); 4 5 %> 6 717 18
TokenServlet.java
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 3 String token = request.getParameter("token"); 4 if("jason".equals(token)){ 5 //清除标记:没有方法清除固定的请求参数 6 } 7 8 }
② Option 2: Put the tag in the request, it doesn’t work. Because after the form page is refreshed, the request has been destroyed, and submitting the form again will be a new request
index.jsp
1 2 <% 3 request.setAttribute("token", "tokenValue"); 4 %> 5 616 17
TokenServlet.java
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 Object token = request.getAttribute("token"); 3 4 if(token != null){ 5 //清除标记:没有方法清除固定的请求参数 6 }else{ 7 response.sendRedirect( request.getContextPath() + "/token/token.jsp"); 8 return; 9 10 } 11 }
③ Solution three: Put the tag into the session. You can. Set the attribute domain value throughhidden fieldandmethod
index.jsp1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 5 6 7 827 28 29 TokenServlet.java避免表单的重复提交 9 10 11 <% 12 13 String tokenValue = new Date().getTime() + ""; 14 session.setAttribute("token", tokenValue ); 15 %> 16 17
1 package com.jason.token.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 13 @WebServlet("/tokenServlet") 14 public class TokenServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 18 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 20 //1.获取session 21 HttpSession session = request.getSession(); 22 23 //2.获取设置好的属性域值 24 Object token = session.getAttribute("token"); 25 26 //3.获取隐藏域的值 27 String tokenValue = request.getParameter("token"); 28 29 //4.处理,若 token不为空,且token 和tokenValue相同 30 if(token != null && token.equals(tokenValue)){ 31 //5.去除标记 32 session.removeAttribute("token"); 33 }else{ 34 //6.重定向到提示页面 35 response.sendRedirect( request.getContextPath() + "/token/token.jsp"); 36 return; 37 } 38 //7.去除标记后,重定向到 成功页面 39 response.sendRedirect( request.getContextPath() + "/token/success.jsp"); 40 41 } 42 43 }success .jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>token.jspInsert title here 成功
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 5 6 74. Summary 1) Using redirection can solve some form submission problems. 2) Prevent repeated submission of forms by combining attribute fields and hidden fields through session. Related articles:Insert title here 8 9 10重复提交了表单
11 12
How to solve the problem of repeated submission of php forms, repeated submission of php forms
javascript - How to solve the problem of refreshing the page form in PHP Repeated submission??
Related videos:JS development verification form tutorial
The above is the detailed content of Javaweb Learning Session Case: How to Solve Repeated Submissions of Forms. For more information, please follow other related articles on the PHP Chinese website!