一、为什么会出现重复提交?
主要是由于网络的延迟问题以及页面刷新的操作。
二、表单的重复提交会导致的问题?
主要能够造成很多脏数据。
三、解决的办法:
3.1 前端解决办法:通过前端的方法将提交按钮变灰。对于前端的办法这里就不做演示了,因为前端的控制虽然能够防止数据的重复提交但是治标不治本。这里主要介绍第二种方法。
3.2 后端解决: 思路:主要是利用唯一Token值与提交参数相匹配验证。
后端解决的代码示例:
1.前端页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/DoFormServlet" method="post">
<input type="hidden" name="token" value="${sessionToken}">
用户名:<input type="text" name="userName"> <input type="submit"
value="提交" id="submit">
</form>
</body>
</html>
2.发送Token值去前端页面代码:
package session;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SendTokenToForm")
public class SendTokenToForm extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 5841829906440324978L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("sessionToken", UUID.randomUUID().toString());
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
3.具体解决重复提交核心代码:
package session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DoFormServlet")
public class DoFormServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 82128771669092572L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!isSubmit(req)) {
resp.getWriter().write("数据已提交");
System.out.println("数据已提交");
}
//让线程休眠0.9秒,方便测试
try {
Thread.sleep(900);
} catch (InterruptedException e) {
e.printStackTrace();
}
String userName = req.getParameter("userName");
System.out.println("正在往数据库插入数据"+userName);
resp.getWriter().write("success");
}
/**
* @Title: isSubmit
* @Description: 判断token值是否相同以及是否有伪token值得传入
* @author 西安工业大学-查文彬
* @time 2017年10月2日 下午5:53:41
* @param request
* @return
* boolean
*/
public boolean isSubmit(HttpServletRequest request){
String sessionToken = (String) request.getSession().getAttribute("sessionToken");
String parameter = request.getParameter("token");
if (!(sessionToken.equals(parameter))) {
return false;
}
request.getSession().removeAttribute("sessionToken");
return true;
}
}
感想:解决数据重复提交虽然技术没有多么高大上,但是由于平时的不怎么注意,很多人都忽略这个细节,解决起来也花不了多少的时间。所以平时对于代码的细节方面应该多多斟酌,追求代码完美。避免不必要的事件发生,即使发生的概率很小。