JAVA防止重复提交Web表单的方法
时间:2020-10-10 11:46:35|栏目:JAVA代码|点击: 次
本文实例讲述了JAVA防止重复提交Web表单的方法。分享给大家供大家参考,具体如下:
package cn.com.form; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sun.misc.BASE64Encoder; //产生表单 public class FormServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //产生随机数 TokenProcessor tp=TokenProcessor.getInstance(); String token=tp.generateToken(); request.getSession().setAttribute("token", token); request.getRequestDispatcher("/form.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } class TokenProcessor//令牌 { /* * 1.把构造函数私有 * 2.自己创建一个 * 3.对外暴露一个方法,允许获取上面创建的对象 * */ private static final TokenProcessor instance=new TokenProcessor(); private TokenProcessor(){} public static TokenProcessor getInstance() { return instance; } public String generateToken() { String token=System.currentTimeMillis()+new Random().nextInt()+""; try { MessageDigest md=MessageDigest.getInstance("md5"); byte[] md5=md.digest(token.getBytes()); //base64编码 BASE64Encoder encoder=new BASE64Encoder(); return encoder.encode(md5); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } }
<%@ 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 'form.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="/Session/DoForm" method="post"> <input type="hidden" name="token" value="${token}"> 用户名:<input type="text" name="userName"> <input type="submit" value="提交"> </form> </body> </html>
package cn.com.form; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DoForm * 处理表单提交的请求 * */ public class DoForm extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*String userName=request.getParameter("userName"); 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; } request.getSession().removeAttribute("token"); System.out.println("向数据库中注册用户=="); } private boolean isTokenValid(HttpServletRequest request) { String client_token=request.getParameter("token"); if(client_token==null) { return false; } String server_token=(String)request.getSession().getAttribute("token"); if(server_token==null) { return false; } if(!client_token.equals(server_token)) { return false; } return true; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
希望本文所述对大家Java web程序设计有所帮助。
上一篇:基于apache poi根据模板导出excel的实现方法
栏 目:JAVA代码
下一篇:简单理解Java的抽象类
本文标题:JAVA防止重复提交Web表单的方法
本文地址:http://www.codeinn.net/misctech/9350.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机