JAVA防止重復(fù)提交Web表單的方法
本文實(shí)例講述了JAVA防止重復(fù)提交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;
//產(chǎn)生表單
public class FormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//產(chǎn)生隨機(jī)數(shù)
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.把構(gòu)造函數(shù)私有
* 2.自己創(chuàng)建一個(gè)
* 3.對(duì)外暴露一個(gè)方法,允許獲取上面創(chuàng)建的對(duì)象
* */
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
* 處理表單提交的請(qǐng)求
*
*/
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("向數(shù)據(jù)庫(kù)提交注冊(cè)用戶...");
*/
boolean b=isTokenValid(request);
if(!b)
{
System.out.println("請(qǐng)不要重復(fù)提交!");
return;
}
request.getSession().removeAttribute("token");
System.out.println("向數(shù)據(jù)庫(kù)中注冊(cè)用戶==");
}
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);
}
}
希望本文所述對(duì)大家Java web程序設(shè)計(jì)有所幫助。
- java如何防止表單重復(fù)提交的注解@RepeatSubmit
- java后端如何實(shí)現(xiàn)防止接口重復(fù)提交
- Java使用注解實(shí)現(xiàn)防止重復(fù)提交實(shí)例
- java后臺(tái)防止表單重復(fù)提交方法詳解
- java開發(fā)中防止重復(fù)提交的幾種解決方案
- Java防止頻繁請(qǐng)求、重復(fù)提交的操作代碼(后端防抖操作)
- Java后端限制頻繁請(qǐng)求和重復(fù)提交的實(shí)現(xiàn)
- Java中防止數(shù)據(jù)重復(fù)提交超簡(jiǎn)單的6種方法
- Java結(jié)合redis實(shí)現(xiàn)接口防重復(fù)提交
- Java表單重復(fù)提交的避免方法
- Java防止重復(fù)提交訂單的實(shí)現(xiàn)示例
相關(guān)文章
使用fileupload組件實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了使用fileupload實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
Java對(duì)象轉(zhuǎn)Json,關(guān)于@JSONField對(duì)象字段重命名和順序問(wèn)題
這篇文章主要介紹了Java對(duì)象轉(zhuǎn)Json,關(guān)于@JSONField對(duì)象字段重命名和順序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Java將時(shí)間戳轉(zhuǎn)換為Date對(duì)象的方法小結(jié)
在?Java?編程中,處理日期和時(shí)間是一個(gè)常見需求,特別是在處理網(wǎng)絡(luò)通信或者數(shù)據(jù)庫(kù)操作時(shí),本文主要為大家整理了Java中將時(shí)間戳轉(zhuǎn)換為Date對(duì)象的方法,希望對(duì)大家有所幫助2024-12-12
Springboot接收Get參數(shù)實(shí)踐過(guò)程
本文主要介紹了在Spring Boot中如何接收不同類型的請(qǐng)求參數(shù),包括在路徑中直接傳遞參數(shù)、跟在問(wèn)號(hào)后面?zhèn)鬟f參數(shù)、使用Map接收參數(shù)、接收數(shù)組以及使用對(duì)象接收參數(shù)等方法2024-12-12
Spring Boot + Mybatis多數(shù)據(jù)源和動(dòng)態(tài)數(shù)據(jù)源配置方法
最近做項(xiàng)目遇到這樣的應(yīng)用場(chǎng)景,項(xiàng)目需要同時(shí)連接兩個(gè)不同的數(shù)據(jù)庫(kù)A, B,并且它們都為主從架構(gòu),一臺(tái)寫庫(kù),多臺(tái)讀庫(kù)。下面小編給大家?guī)?lái)了Spring Boot + Mybatis多數(shù)據(jù)源和動(dòng)態(tài)數(shù)據(jù)源配置方法,需要的朋友參考下吧2018-01-01
SpringBoot整合atomikos實(shí)現(xiàn)跨庫(kù)事務(wù)的詳細(xì)方案
這篇文章主要介紹了SpringBoot整合atomikos實(shí)現(xiàn)跨庫(kù)事務(wù),業(yè)務(wù)主要涉及政府及企業(yè)且并發(fā)量不大,所以采用XA事務(wù),雖然性能有所損失,但是可以保證數(shù)據(jù)的強(qiáng)一致性,需要的朋友可以參考下2022-06-06
SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由
這篇文章主要介紹了SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
JAVA實(shí)現(xiàn)微信APPV3支付保姆級(jí)教程
微信實(shí)現(xiàn)支付功能與支付寶實(shí)現(xiàn)支付功能是相似的,這篇文章主要介紹了JAVA實(shí)現(xiàn)微信APPV3支付的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

