日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

客户端服务端防止用户重复提交表单

發布時間:2023/12/3 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 客户端服务端防止用户重复提交表单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、什么是表單重復提交?
當網絡有延遲時,用戶提交的表單等數據還沒有完成此次提交,但用戶又多次點擊提交,造成用戶數據在數據庫或存儲中被提交多次。
利用線程延遲,簡單模擬重復提交。
表單頁面為form.html
[html] view plain copy



form.html

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->



用戶名 :



處理提交請求的servlet為DoFormServlet.java
[java] view plain copy
package SessionDemo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DoFormServlet extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); //利用線程休眠,模擬網絡延遲 try { Thread.sleep(1000*3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("向數據庫中注冊數據。"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

}
在瀏覽器中加載form.html

當用戶反復點擊“提交”時,就造成了重復提交。

二、解決方法一:利用javascript阻止
其他不變,將form.html修改為
[html] view plain copy



form.html

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->


var isCommitted = false;
function dosubmit()
{
if(!isCommitted)
{
isCommitted=true;
return true;
}
else
{
return false;
}
}




用戶名 :




這樣在瀏覽器中加載form.html后,點擊“提交”,終端中只會輸出一次”向數據庫中注冊數據。”,表明成功阻止表單反復提交。
以上form.html可以進一步優化,當用戶點擊“提交”后,“提交”按鈕應該變為灰色不可用。
[html] view plain copy



form.html

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->


function dosubmit()
{
var input = document.getElementById("submit");
input.disabled = 'disabled';
return true;
}




用戶名 :




利用javascript的方法不能完全防止用戶惡意重復提交,例如:用戶可以將form.html保存后修改,還可以在點擊“提交”后重復刷新頁面,從而實現反復提交。
三、解決方法二:利用服務器端Session防止表單重復提交。
其中FormServlet.java為
[java] view plain copy
package SessionDemo;

import java.io.IOException;
import java.io.PrintWriter;
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
{

public 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); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

}

//設計為單例模式
class TokenProcessor
{
private TokenProcessor(){};
private static final TokenProcessor instance = new 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) { throw new RuntimeException(e); } }

}
添加form.jsp
[plain] view plain copy
<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8”%>







用戶名




將DoFormServlet.java修改為
[java] view plain copy
package SessionDemo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DoFormServlet extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); boolean b = isTokenValue(request); if (!b) { System.out.println("請不要重復提交。"); return; } request.getSession().removeAttribute("token"); System.out.println("向數據庫中注冊數據。"); } //判斷表單號是否有效 private boolean isTokenValue(HttpServletRequest request) { String clientToken = request.getParameter("token"); if(clientToken==null) { return false; } String serverToken = (String) request.getSession().getAttribute("token"); if(serverToken==null) { return false; } if (!clientToken.equals(serverToken)) { return false; } return true; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

}
再瀏覽器中加載FormServlet

點擊“提交”后跳轉

終端顯示用戶提交

這時即使用戶點擊“刷新”,也不能實現重復提交。

總結

以上是生活随笔為你收集整理的客户端服务端防止用户重复提交表单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。