前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java后端解决重复提交问题

java后端解决重复提交问题

作者头像
林老师带你学编程
发布2019-05-26 00:04:19
9390
发布2019-05-26 00:04:19
举报
文章被收录于专栏:强仔仔强仔仔

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1434235

一、为什么会出现重复提交?

主要是由于网络的延迟问题以及页面刷新的操作。

二、表单的重复提交会导致的问题?

主要能够造成很多脏数据。

三、解决的办法:

3.1 前端解决办法:通过前端的方法将提交按钮变灰。对于前端的办法这里就不做演示了,因为前端的控制虽然能够防止数据的重复提交但是治标不治本。这里主要介绍第二种方法。

3.2 后端解决: 思路:主要是利用唯一Token值与提交参数相匹配验证。

后端解决的代码示例:

1.前端页面

代码语言:javascript
复制
<%@ 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值去前端页面代码:

代码语言:javascript
复制
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.具体解决重复提交核心代码:

代码语言:javascript
复制
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;
	}

}

感想:解决数据重复提交虽然技术没有多么高大上,但是由于平时的不怎么注意,很多人都忽略这个细节,解决起来也花不了多少的时间。所以平时对于代码的细节方面应该多多斟酌,追求代码完美。避免不必要的事件发生,即使发生的概率很小。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年10月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档