10-客户端防表单重复提交和服务器端session防表单重复提交
/****************************************************DoFormServlet********************************************************/
package session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DoFormServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*String name = request.getParameter("user");
try {
Thread.sleep(1000*3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//模拟向数据库注册用户
System.out.println("向数据库注册用户");*/
boolean b = isTokenValid(request);
if(!b){
System.out.println("请不要重复提交");
return;
}
//在向数据库中提交之前要remove掉表单号
request.getSession().removeAttribute("token");
System.out.println("向数据库注册用户");
}
//判断表单号是否有效
private boolean isTokenValid(HttpServletRequest request) {
//得到客户机带过来的表单号
String clientToken = request.getParameter("token");
//判断客户机是否带表单号过来
//如果没带过来,我也认为你是重复提交
if(clientToken==null){
return false;
}
//判断服务器里有没有表单号
String serverToken = (String) request.getSession().getAttribute("token");
//服务端里如果没有的话,也不行
if(serverToken == null){
return false;
}
//客户端和服务端不想等的话也不行
if(!clientToken.equals(serverToken)){
return false;
}
return true;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
/*********************************************************************************form.jsp***********************************************/
<%@ 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>
<form action="/day07/DoFormServlet" method="post">
<input type="hidden" name="token" value="${token}">
用户名:<input type="text" name="username"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
/****************************************************************DoFormServlet**************************************************************/
package session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DoFormServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*String name = request.getParameter("user");
try {
Thread.sleep(1000*3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//模拟向数据库注册用户
System.out.println("向数据库注册用户");*/
boolean b = isTokenValid(request);
if(!b){
System.out.println("请不要重复提交");
return;
}
//在向数据库中提交之前要remove掉表单号
request.getSession().removeAttribute("token");
System.out.println("向数据库注册用户");
}
//判断表单号是否有效
private boolean isTokenValid(HttpServletRequest request) {
//得到客户机带过来的表单号
String clientToken = request.getParameter("token");
//判断客户机是否带表单号过来
//如果没带过来,我也认为你是重复提交
if(clientToken==null){
return false;
}
//判断服务器里有没有表单号
String serverToken = (String) request.getSession().getAttribute("token");
//服务端里如果没有的话,也不行
if(serverToken == null){
return false;
}
//客户端和服务端不想等的话也不行
if(!clientToken.equals(serverToken)){
return false;
}
return true;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
10-客户端防表单重复提交和服务器端session防表单重复提交的更多相关文章
- java_客户端防表单重复提交和服务器端session防表单重复提交
用户输入FormServlet链接 FormServlet-〉form.jsp->DoFormServlet FormServlet:产生token,放在session中 form.jsp:hi ...
- JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传
Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...
- java web学习总结(十三) -------------------使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- [Java拾遗五]使用Session防止表单重复提交
申明:此文章属于转载, 转自博客: http://www.cnblogs.com/xdp-gacl/p/3859416.html在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没 ...
- JavaWeb学习总结(十三)——使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- (转)JavaWeb学习总结(十三)——使用Session防止表单重复提交
如何防止表单重复提交 在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复 ...
- JavaWeb学习 (十二)————使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- 利用session防止表单重复提交
转自:http://www.cnblogs.com/xdp-gacl/p/3859416.html 利用Session防止表单重复提交 对于[场景二]和[场景三]导致表单重复提交的问题,既然客户端无法 ...
- JavaWeb(十三)——使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
随机推荐
- codechef AUG17 T4 Palindromic Game
Palindromic Game Problem Code: PALINGAM There are two players A, B playing a game. Player A has a st ...
- set基本用法-----2
#include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> #inclu ...
- LOJ#2084. 「NOI2016」网格
$n,m \leq 1e9$,$n*m$的网格中有$c \leq 1e5$个是黑的,其他是白的.问:使至少两个白的不连通,最少需要再把几个白的涂黑. 可以发现答案是-1,0,1,2啦.-1要么没白的, ...
- Linux Performance Observability Tools
- HDU——最大连续子序列(区间DP)
上一个题的加强版! 最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Arduino可穿戴教程认识ArduinoIDE
Arduino可穿戴教程认识ArduinoIDE 认识ArduinoIDE Arduino IDE在Windows和Linux平台下除了启动方式之外,其他的使用方式基本是一致的.下面简单介绍一下常用的 ...
- Hdoj 5181 numbers
numbers Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 196608/196608 K (Java/Others)Total ...
- java之properties和yml两种配置文件比较(非原创)
文章大纲 一.内容介绍二.参考文章 一.内容介绍 我们在使用SpringBoot这个框架的时候都一定使用或者说是见到过application.properties或者是application.ym ...
- zoj 2615 Cells 栈的运用
题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...
- MySQL OCP
http://www.royalwzy.com/ http://www.aixchina.net/home/space.php?uid=898169