servlet一般防止表单重复提交
2016-05-16 00:00
274 查看
摘要: servlet一般防止表单重复提交
页面----------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'token.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">
</head>
<body>
<%
//生成一个formhash,算法可以自己定,不随便重复就可以了
Random ran = new Random();
String formhash = String.valueOf(ran.nextInt());
//读取当前session里面的hashCode集合,此处使用了Set,方便判断。
Set<String> formhashSession = (Set<String>) session.getAttribute("formhashSession");
if (formhashSession == null) {
formhashSession = new HashSet<String>();
}
// 检测重复问题
while (formhashSession.contains(formhash)) {
formhash = String.valueOf(ran.nextInt());
}
// 保存到session里面
formhashSession.add(formhash);
// 保存
session.setAttribute("formhash", formhashSession);
%>
<form action="/tokenWeb/TokenServlet">
<input type="hidden" name="formhash" id="formhash" value="<%=formhash%>" />
<input type="submit" value="提交" />
</form>
</body>
</html>
-------------------------servlet-----------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
HttpSession session=request.getSession();
String formhash = request.getParameter("formhash");
// 拿到session里面的集合
Set<String> formhashSession = (Set<String>) session.getAttribute("formhash");
// 如果没有,则是重复提交,或者非法提交
Thread.sleep(2000);
if (formhashSession == null || !formhashSession.contains(formhash) ) {
System.out.println("重复提交!");
}else{
System.out.println("正常");
}
// 最后,如果操作成功,从session里面把这个formhash 删掉!
formhashSession.remove(formhash);
session.setAttribute("formhashSession", formhashSession);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
页面----------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'token.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">
</head>
<body>
<%
//生成一个formhash,算法可以自己定,不随便重复就可以了
Random ran = new Random();
String formhash = String.valueOf(ran.nextInt());
//读取当前session里面的hashCode集合,此处使用了Set,方便判断。
Set<String> formhashSession = (Set<String>) session.getAttribute("formhashSession");
if (formhashSession == null) {
formhashSession = new HashSet<String>();
}
// 检测重复问题
while (formhashSession.contains(formhash)) {
formhash = String.valueOf(ran.nextInt());
}
// 保存到session里面
formhashSession.add(formhash);
// 保存
session.setAttribute("formhash", formhashSession);
%>
<form action="/tokenWeb/TokenServlet">
<input type="hidden" name="formhash" id="formhash" value="<%=formhash%>" />
<input type="submit" value="提交" />
</form>
</body>
</html>
-------------------------servlet-----------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
HttpSession session=request.getSession();
String formhash = request.getParameter("formhash");
// 拿到session里面的集合
Set<String> formhashSession = (Set<String>) session.getAttribute("formhash");
// 如果没有,则是重复提交,或者非法提交
Thread.sleep(2000);
if (formhashSession == null || !formhashSession.contains(formhash) ) {
System.out.println("重复提交!");
}else{
System.out.println("正常");
}
// 最后,如果操作成功,从session里面把这个formhash 删掉!
formhashSession.remove(formhash);
session.setAttribute("formhashSession", formhashSession);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
相关文章推荐
- struts2防止重复提交
- struts2小案例 包括防止重复提交 转换器
- php中的自动加载类机制原理
- php中的session和cookie机制
- IntelliJ IDEA Community版独特的一个bug:某些目录下的文件能显示但不被编译
- TP的四种路由模式
- 促使IDC市场发展的霸王龙
- JS的并行加载与执行顺序
- 如何编辑MathType公式波浪线
- MathType怎么编辑化学等式
- 怎么编辑MathType集合运算符号
- 使用Druid对数据库密码加密
- SVN中分支与合并
- C 侵入式数据结构
- SVN常用命令
- git常用命令
- JAVA gc垃圾回收机制
- 几个关于Java内存泄露方面的问题
- 关于RPC与MQ异同的理解
- DotNet Core CLI Samples on Windows