码迷,mamicode.com
首页 > 其他好文 > 详细

防止表单重复提交的问题

时间:2018-07-24 00:46:19      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:www   bubuko   姓名   mapping   version   带来   chain   cep   ec2   

1.什么是重复提交及重复提高带来的什么问题?:很严重

2.解决方案:

  方案一.利用javaScript

  技术分享图片

  方案二.请求重定向到另一个界面:Redirect After (Google):

  方案三:利用HTTPSession防止表单的重复提交(推荐使用):

      技术分享图片

技术分享图片
 1 import java.io.IOException;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import javax.servlet.http.HttpSession;
 8 
 9 public class RegistServlet extends HttpServlet {
10 
11     public void doGet(HttpServletRequest request, HttpServletResponse response)
12             throws ServletException, IOException {
13         
14         String iToken = request.getParameter("token");
15         HttpSession session = request.getSession();
16         String sToken = (String) session.getAttribute("token");
17         if(iToken.equals(sToken)){
18             String name = request.getParameter("name");
19             System.out.println(name);
20             response.getWriter().write("sucess");
21             session.removeAttribute("token");
22         }else{
23             response.getWriter().write("resubmit occured");
24         }
25     }
26 
27     public void doPost(HttpServletRequest request, HttpServletResponse response)
28             throws ServletException, IOException {
29         doGet(request, response);
30     }
31 
32 }
RegistServlet
技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>
 5     <title>title</title> 
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9     <!--
10     <link rel="stylesheet" type="text/css" href="styles.css">
11     -->
12 
13   </head>
14   
15   <body>
16        保存成功
17   </body>
18 </html>
Success.jsp
技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>
 5     <title>新用户注册</title> 
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9     <!--
10     <link rel="stylesheet" type="text/css" href="styles.css">
11     -->
12 
13   </head>
14   
15   <body>
16       <%
17       String token = UUID.randomUUID().toString();
18       pageContext.setAttribute("token", token);
19       session.setAttribute("token", token);
20       %>
21     <form action="${pageContext.request.contextPath}/servlet/RegistServlet" method="post">
22         <input type="hidden" name="token" value="${token}"/>
23         姓名:<input type="text" name="name"/><br/>
24         <input type="submit" value="注册"/>
25     </form>
26     <script type="text/javascript">
27         window.onload=function(){
28             document.getElementById("btn1").onclick=function(){
29                 document.forms[0].submit();
30                 document.getElementById("btn1").disabled=true;
31             }
32         }
33     </script>
34   </body>
35 </html>
regist.jsp

自定义标签防止重复提交表单:

 标签:

技术分享图片
 1 import java.io.IOException;
 2 import java.util.UUID;
 3 
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.JspWriter;
 6 import javax.servlet.jsp.PageContext;
 7 import javax.servlet.jsp.tagext.SimpleTagSupport;
 8 
 9 public class TokenSimpleTag extends SimpleTagSupport {
10     //向HttpSession中放一个令牌;输出input hidden的表单域
11     public void doTag() throws JspException, IOException {
12         try {
13             String token = UUID.randomUUID().toString();
14             PageContext pc = (PageContext)getJspContext();
15             pc.getSession().setAttribute("token", token);
16             JspWriter out = pc.getOut();
17             out.write("<input type=\"hidden\" name=\"token\" value=\""+token+"\"/>");
18         } catch (Exception e) {
19             throw new RuntimeException(e);
20         }
21     }
22     
23 }
TokenSimpleTag

tld文件:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <taglib xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
 5     version="2.1">
 6     
 7   <tlib-version>1.0</tlib-version>
 8   <short-name>itheima</short-name>
 9   <uri>http://www.itheima.com/framework/core</uri>
10   <tag>
11       <name>token</name>
12       <tag-class>com.itheima.tag.TokenSimpleTag</tag-class>
13       <body-content>empty</body-content>
14   </tag>
15 </taglib>
itheima.tld

xml文件:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <filter>
 9       <filter-name>TokenFilter</filter-name>
10       <filter-class>com.itheima.filter.TokenFilter</filter-class>
11   </filter>
12   <filter-mapping>
13       <filter-name>TokenFilter</filter-name>
14       <url-pattern>/servlet/*</url-pattern>
15   </filter-mapping>
16   <servlet>
17       <servlet-name>RegistServlet</servlet-name>
18       <servlet-class>com.itheima.servlet.RegistServlet</servlet-class>
19   </servlet>
20   <servlet-mapping>
21       <servlet-name>RegistServlet</servlet-name>
22       <url-pattern>/servlet/RegistServlet</url-pattern>
23   </servlet-mapping>
24   <welcome-file-list>
25     <welcome-file>index.jsp</welcome-file>
26   </welcome-file-list>
27 </web-app>
web.xml

过滤器:

技术分享图片
 1 import java.io.IOException;
 2 
 3 import javax.servlet.Filter;
 4 import javax.servlet.FilterChain;
 5 import javax.servlet.FilterConfig;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletRequest;
 8 import javax.servlet.ServletResponse;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 public class TokenFilter implements Filter {
14 
15     public void init(FilterConfig filterConfig) throws ServletException {
16 
17     }
18 
19     public void doFilter(ServletRequest req, ServletResponse resp,
20             FilterChain chain) throws IOException, ServletException {
21         HttpServletRequest request;
22         HttpServletResponse response;
23         try{
24             request = (HttpServletRequest)req;
25             response = (HttpServletResponse)resp;
26         }catch(Exception e){
27             throw new RuntimeException(e);
28         }
29         
30         String iToken = request.getParameter("token");
31         HttpSession session = request.getSession();
32         String sToken = (String) session.getAttribute("token");
33         if(iToken==null){
34             throw new RuntimeException("form must use itheima:token");
35         }
36         if(iToken.equals(sToken)){
37             session.removeAttribute("token");
38             chain.doFilter(request, response);
39         }else{
40             response.getWriter().write("resubmit occured");
41         }
42     }
43 
44     public void destroy() {
45 
46     }
47 
48 }
TokenFilter.java

Servlet文件:

技术分享图片
 1 import java.io.IOException;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import javax.servlet.http.HttpSession;
 8 
 9 public class RegistServlet extends HttpServlet {
10 
11     public void doGet(HttpServletRequest request, HttpServletResponse response)
12             throws ServletException, IOException {
13             System.out.println(request.getParameter("name"));
14     
15     }
16 
17     public void doPost(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         doGet(request, response);
20     }
21 
22 }
RegistServle.java

jsp文件:

技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://www.itheima.com/framework/core" prefix="itheima"%>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>新用户注册</title> 
 7     <meta http-equiv="pragma" content="no-cache">
 8     <meta http-equiv="cache-control" content="no-cache">
 9     <meta http-equiv="expires" content="0">    
10     <!--
11     <link rel="stylesheet" type="text/css" href="styles.css">
12     -->
13 
14   </head>
15   
16   <body>
17     <form action="${pageContext.request.contextPath}/servlet/RegistServlet" method="post">
18         姓名:<input type="text" name="name"/><br/>
19         <input type="submit" value="注册"/>
20         <itheima:token/>
21     </form>
22   </body>
23 </html>
Regist.jsp
技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>
 5     <title>title</title> 
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9     <!--
10     <link rel="stylesheet" type="text/css" href="styles.css">
11     -->
12 
13   </head>
14   
15   <body>
16        保存成功
17   </body>
18 </html>
success.jsp

 

防止表单重复提交的问题

标签:www   bubuko   姓名   mapping   version   带来   chain   cep   ec2   

原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9356591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!