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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

表单的重复提交问题解决方案

發(fā)布時間:2025/5/22 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 表单的重复提交问题解决方案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.在我們的web開發(fā)中,常常會遇到表單的重復提交問題,那么我們的解決方案有兩種:①重定向:response.sendrediect(); ②token的使用,即做個標記


下面寫一個token的例子:(在tomcat上可以運行的。)

1.JSP頁面,3個。

index.jsp

  • <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  • <html>

  • <head>

  • <title>避免表單重復提交的例子</title>

  • </head>

  • <body>

  • <h4>請支付:</h4>

  • <%

  • ? ?String token = (String)request.getAttribute("mytoken");

  • ? ? ? ?out.print(token);

  • ? ?%>

  • <formaction="token.jspx?_m=pay"method="post">

  • <inputtype="hidden"name="token"value="<%=token %>"/>

  • <inputtype="text"name="money"/>

  • <inputtype="submit"value="支付"/>

  • </form>

  • </body>

  • </html>

  • suc.jsp

  • <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  • <html>

  • <head>

  • <title>支付成功</title>

  • </head>

  • <body>

  • <h2style="color:red;">支付成功!感謝您的使用!</h2>

  • </body>

  • </html>

  • error.jsp

  • <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  • <html>

  • <head>

  • <title>重復提交了</title>

  • </head>

  • <body>

  • <h2>系統(tǒng)正在處理,請勿重復提交!</h2>

  • </body>

  • </html>

  • 2.com.hanchao.web.util是我們工具包

  • package com.hanchao.web.util;

  • import java.util.UUID;

  • import javax.servlet.http.HttpServletRequest;

  • import javax.servlet.http.HttpSession;

  • /**

  • * 工具類

  • * @author hanlw

  • * 2012-07-09

  • */

  • publicclass TokenUtil {

  • //定義一個常量,此常量只是為以后取值,傳值方便

  • privatestaticfinal String TOKEN = "TOKEN";

  • /**

  • ? ? * 獲得唯一的token

  • ? ? * @param request

  • ? ? * @return

  • ? ? */

  • public String getToken(HttpServletRequest request) {

  • //★UUID可以產生唯一的序列碼

  • UUID uuid = UUID.randomUUID();

  • ? ? ? ?String token = uuid.toString();

  • ? ? ? ?HttpSession session = request.getSession();

  • ? ? ? ?session.setAttribute(TOKEN, token);

  • return token;

  • ? ?}

  • /**

  • ? ? * 驗證token

  • ? ? * @param request

  • ? ? * @param requestToken

  • ? ? * @return

  • ? ? */

  • publicboolean validateToken(HttpServletRequest request,String requestToken) {

  • ? ? ? ?HttpSession session = request.getSession();

  • ? ? ? ?String sessionToken = (String) session.getAttribute(TOKEN);

  • if(sessionToken != null &&

  • ? ? ? ? ? requestToken != null &&

  • ? ? ? ? ? sessionToken.equals(requestToken)) {

  • ? ? ? ? ? ?session.removeAttribute(TOKEN);

  • returntrue;

  • ? ? ? ?} else {

  • returnfalse;

  • ? ? ? ?}

  • ? ?}

  • }

  • 3.我們的servlet

  • package com.hanchao.web.util;

  • import java.io.IOException;

  • import javax.servlet.ServletException;

  • import javax.servlet.http.HttpServlet;

  • import javax.servlet.http.HttpServletRequest;

  • import javax.servlet.http.HttpServletResponse;

  • publicclass TokenServlet extends HttpServlet {

  • privatestaticfinallong serialVersionUID = 1L;

  • publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

  • throws ServletException, IOException {

  • ? ? ? ?String method = request.getParameter("_m");

  • if("pay".equals(method)) {

  • ? ? ? ? ? ?pay(request,response);

  • ? ? ? ?} elseif("tosave".equals(method)) {

  • ? ? ? ? ? ?save(request,response);

  • ? ? ? ?}

  • ? ?}

  • /**

  • ? ? * 產生token

  • ? ? * @param request

  • ? ? * @param response

  • ? ? * @throws ServletException

  • ? ? * @throws IOException

  • ? ? */

  • privatevoid save(HttpServletRequest request, HttpServletResponse response) ?

  • throws ServletException, IOException{

  • ? ? ? ?TokenUtil util = new TokenUtil();

  • ? ? ? ?String token = util.getToken(request);

  • ? ? ? ?request.setAttribute("mytoken", token);

  • ? ? ? ?request.getRequestDispatcher("index.jsp").forward(request, response);

  • ? ?}

  • /**

  • ? ? * 支付

  • ? ? * @param request

  • ? ? * @param response

  • ? ? * @throws ServletException

  • ? ? * @throws IOException

  • ? ? */

  • privatevoid pay(HttpServletRequest request, HttpServletResponse response)

  • throws ServletException, IOException{

  • ? ? ? ?String token = request.getParameter("token");

  • ? ? ? ?TokenUtil util = new TokenUtil();

  • boolean result = util.validateToken(request, token);

  • if(result) {

  • ? ? ? ? ? ?String money = request.getParameter("money");

  • ? ? ? ? ? ?System.out.println("支付"+money+"成功");

  • ? ? ? ? ? ?request.getRequestDispatcher("suc.jsp").forward(request, response);

  • ? ? ? ?} else {

  • ? ? ? ? ? ?request.getRequestDispatcher("error.jsp").forward(request, response);

  • ? ? ? ?}

  • ? ?}

  • publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

  • throws ServletException, IOException {

  • ? ? ? ?doGet(request,response);

  • ? ?}

  • }



  • ==================================================================

    2013-06-21-add-han

    昨天和我們的架構聊到這個問題,因為現(xiàn)在做的項目涉及到了。他說:對于分布式的大型互聯(lián)網項目,這種token的方式,在高并發(fā)時可能會出現(xiàn)問題。所以,也建議用redirect最保險吧。謝謝













    轉載于:https://blog.51cto.com/hanchaohan/931145

    總結

    以上是生活随笔為你收集整理的表单的重复提交问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

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