JAVA防止重復(fù)提交Web表單的方法
更新時(shí)間:2015年10月19日 14:22:44 作者:煙大洋仔
這篇文章主要介紹了JAVA防止重復(fù)提交Web表單的方法,涉及Java針對表單的相關(guān)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(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.對外暴露一個(gè)方法,允許獲取上面創(chuàng)建的對象 * */ 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 * 處理表單提交的請求 * */ 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ù)庫提交注冊用戶..."); */ boolean b=isTokenValid(request); if(!b) { System.out.println("請不要重復(fù)提交!"); return; } request.getSession().removeAttribute("token"); System.out.println("向數(shù)據(jù)庫中注冊用戶=="); } 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程序設(shè)計(jì)有所幫助。
相關(guān)文章
springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式
這篇文章主要介紹了springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Java基于socket實(shí)現(xiàn)簡易聊天室實(shí)例
這篇文章主要介紹了Java基于socket實(shí)現(xiàn)簡易聊天室的方法,實(shí)例分析了java基于socket實(shí)現(xiàn)聊天室服務(wù)端與客戶端的相關(guān)技巧,需要的朋友可以參考下2015-05-05Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))
這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)的相關(guān)資料,其中包括單向鏈表、雙向鏈表及鏈表反轉(zhuǎn)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2021-06-06Spring Boot整合Spring Data JPA過程解析
這篇文章主要介紹了Spring Boot整合Spring Data JPA過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-105分鐘快速創(chuàng)建spring boot項(xiàng)目的完整步驟
這篇文章主要給大家介紹了關(guān)于通過5分鐘快速創(chuàng)建spring boot項(xiàng)目的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06