激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - JAVA防止重復提交Web表單的方法

JAVA防止重復提交Web表單的方法

2020-01-10 16:22煙大洋仔 JAVA教程

這篇文章主要介紹了JAVA防止重復提交Web表單的方法,涉及Java針對表單的相關處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了JAVA防止重復提交Web表單的方法。分享給大家供大家參考,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩毛片网站 | 视频一区二区三区在线播放 | 制服丝袜日日夜夜 | 日韩精品羞羞答答 | 欧美一级淫片a免费播放口 91九色蝌蚪国产 | 九九热视频这里只有精品 | 精品免费久久 | 久久久www视频 | 亚洲一二区视频 | 深夜影院a | 日韩精品二区 | 久久69精品久久久久久国产越南 | 亚洲日色| 久久久久久艹 | 香蕉在线播放 | 中文字幕免费在线观看视频 | 538任你躁在线精品视频网站 | 久久新网址 | 日本在线播放一区二区 | 中文字幕精品一区久久久久 | 日本aaaa片毛片免费观看视频 | 超碰在线97国产 | 91小视频在线观看免费版高清 | a一级黄色大片 | 日本在线观看视频网站 | 中文字幕国产欧美 | 国产精品自拍99 | 色淫湿视频 | 国产午夜网 | sese在线视频| 国产精品久久久久久久久久免 | 国产精品午夜未成人免费观看 | 黄色高清免费网站 | 天天看成人免费毛片视频 | 国产一区二区三区四区五区在线 | 西川av在线一区二区三区 | 色人久久 | 91看点 | 亚洲av一级毛片特黄大片 | 免费看污视频在线观看 | 视频一区二区三区免费观看 |