最近在看些基础的东西,顺便做下笔记。相信大家在平时网页使用中,经常会有按钮重复点击,然后点不动刷新,还有当网络延时比较厉害点了没反应在点击的重复提交。为了避免这种情况,总结了一下4点处理方案
表单重复提交 常用解决办法。
1.通过前端解决(请参考以下的前端代码)
2.让提交按钮只触发一次(当用户点击提交后,再次点击的时候不允许调用后台接口,和3类似)
3.提交按钮变灰
4.通过后端解决,使用token+session
思路是:
第一步:
用户输入URL地址,调用第一个后台接口,代码如下
package
com.itmayiedu.servlet;
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;
import
javax.servlet.http.HttpSession;
import com.itmayiedu.servlet.session.TokenUtils;
@WebServlet("/LocaFromServlet")
public
class
LocaFromServlet
extends
HttpServlet {
@Override
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
// 生成token
String tokenValue=TokenUtils.
getToken
();
HttpSession session = req.getSession();
session.setAttribute("sessionToken", tokenValue);
req.getRequestDispatcher("from.jsp").forward(req, resp);
}
}
第二步:将第一步生成的sessionToken转发到前端后,前端的代码
<%@ 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>
<title>Form表单</title>
<script type=
"text/javascript">
var flag =
false;//false表示为提交 true 就是已经提交
function dosubmit(){
//获取表单提交按钮
var btnSubmit = document.getElementById("submit");
//将表单提交按钮设置为不可用,这样就可以避免用户再次点击提交按钮
btnSubmit.disabled= "disabled";
//返回true让表单可以正常提交
return true;
}
</script>
</head>
<body>
<form action=
"DoFormServlet"
method=
"post" οnsubmit="return dosubmit()">
<input type=
"hidden" name=
"parameterToken" value=
"${sessionToken}
">
用户名:<input type=
"text" name=
"userName"> <input type=
"submit"
value=
"提交" id=
"submit" >
</form>
</body>
</html>
第三步:前端进行提交后,后台要判断前端传过来的sessionToken是否和后台的sessionToken一样。一样的话可以提交,不一样就说明重复提交了,不能提交
package com.itmayiedu.servlet;
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 {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");// 防止浏览器显示乱码
if(!isBumit(req)){
System.out.println("您提交提交了数据..或者token错误!");
resp.getWriter().write("您提交提交了数据..或者token错误!");
return ;
}
String userName = req.getParameter("userName");
try {
Thread.sleep(300);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("数据库插入数据...userName:" + userName);
// 插入数据库...
resp.getWriter().write("保存成功..");
req.getSession().removeAttribute("sessionToken");
}
public Boolean isBumit(HttpServletRequest request) {
String parameterToken = request.getParameter("parameterToken");//通过前端传的sessionToken
String sessionToken = (String) request.getSession().getAttribute("sessionToken");//根据后台生成的sessionToken
//判断是否提交
if (sessionToken == null) {
return false;
}
// 判断是否是伪造token
if(!(parameterToken.equals(sessionToken))){
return false;
}
return true;
}
}
所使用到的工具类是生成Token的TokenUtils,其实就是用UUID
package com.itmayiedu.servlet.session;
import java.util.UUID;
public
class TokenUtils {
static
public String getToken() {
return UUID.
randomUUID().toString();
}
public
static
void main(String[] args) {
System.
out.println(TokenUtils.
getToken());
}