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 by request 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: , not feasible, reason: //Clear mark: There is no way to clear fixed request parameters
index.jsp
1 <body> 2 <% 3 request.setAttribute("token", "tokenValue"); 4 5 %> 6 7 <form action="<%= request.getContextPath() %>/tokenServlet" method="post"> 8 9 <input type="hidden" name="token" value="jason"/> 10 11 12 13 name:<input type="text" name="name"/> 14 <input type="submit" value="submit"/> 15 16 </form> 17 18 </body>
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 <body> 2 <% 3 request.setAttribute("token", "tokenValue"); 4 %> 5 6 <form action="<%= request.getContextPath() %>/tokenServlet" method="post"> 7 8 <input type="hidden" name="token" /> 9 10 11 12 name:<input type="text" name="name"/> 13 <input type="submit" value="submit"/> 14 15 </form> 16 17 </body>
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 through hidden field and method
index.jsp1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>避免表单的重复提交</title> 9 </head> 10 <body> 11 <% 12 13 String tokenValue = new Date().getTime() + ""; 14 session.setAttribute("token", tokenValue ); 15 %> 16 17 <form action="<%= request.getContextPath() %>/tokenServlet" method="post"> 18 <!-- 隐藏域 --> 19 <input type="hidden" name="token" value="<%= tokenValue %>"/> 20 21 22 23 name:<input type="text" name="name"/> 24 <input type="submit" value="submit"/> 25 26 </form> 27 28 </body> 29 </html>
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 }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>成功</h4> </body> </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h4> 重复提交了表单 </h4> 11 </body> 12 </html>
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!