表单重复提交问题

一、常见的重复提交问题 
  a>点击提交按钮两次。 
  b>点击刷新按钮。 
  c>使用浏览器后退按钮重复之前的操作,导致重复提交表单。 
  d>使用浏览器历史记录重复提交表单。 
  e>浏览器重复的HTTP请求。

 

二、防止表单重复提交原理

  提交表单的时候提交一份随机的字符串或随机数字等等,再把这个随机的数据存到request里面,然后把表单数据提交,在后台验证的时候判断提交的这两份额外的数据是否一致,如果一致,则把其中一份删除掉,这么做的目的是防止再次提交,继续进行操作,如果不一致,则返回一个响应的页面进行提示!

 

三、代码

  项目目录

  

 

login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>表单重复提交问题</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21     <%
22         String tokenValue = new Date().getTime() + ""; 
23     %>
24   </head>
25   
26   <body>
27     表单重复提交问题<hr>
28     <form action="<%=path %>/tokenServlet" method="post">
29         username    :<input type="text" name="username" /><br>
30         password    :<input type="password" type="password" size="20"/><br>
31         
32         <input type="hidden" name="token" value="<%=tokenValue%>" />
33         <%
34             session.setAttribute("token", tokenValue);
35          %>
36         
37         <input type="submit" value="登录">
38     </form>
39   </body>
40 </html>

  

success.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24    登陆成功,欢迎您,<%=request.getAttribute("username") %>
25   </body>
26 </html>

 

 

 

 

  token.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24   请不要重复提交表单数据!<br>
25   </body>
26 </html>

 

 

 

 

  TokenServlet.java

 1 package com.xjh.form;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 
12 public class TokenServlet extends HttpServlet {
13 
14     public void doGet(HttpServletRequest request, HttpServletResponse response)
15             throws ServletException, IOException {
16         doPost(request, response);
17     }
18 
19     public void doPost(HttpServletRequest request, HttpServletResponse response)
20             throws ServletException, IOException {
21         HttpSession session = request.getSession();
22         Object token = session.getAttribute("token");                //session中的token
23         String tokenValue = request.getParameter("token");            //表单提交的隐藏数据token
24         System.out.println(token);        //第二次进来的时候这个会输出null
25         System.out.println(tokenValue);
26         
27         if(token != null && token.equals(tokenValue)){        //第一次进来符合,把数据移除,第二次进来不符合
28             session.removeAttribute("token");
29         }else {
30             response.sendRedirect(request.getContextPath() + "/token/token.jsp");    //请求转发
31             return ;
32         }
33         
34         String username = request.getParameter("username");
35         request.setAttribute("username", username);
36         System.out.println("username = " + username);
37         request.getRequestDispatcher("/token/success.jsp").forward(request, response);    //请求转发
38         
39 //        response.sendRedirect(request.getContextPath() + "/token/success.jsp");            //请求重定向
40     }
41 }

 

 

 

 

访问:http://127.0.0.1:8080/demo-form/tokenServlet

 

 

http://www.cnblogs.com/Java-web-wy/

posted @ 2017-02-04 20:52  技术猿生活高生活  阅读(417)  评论(0编辑  收藏  举报