日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

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

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


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

1.JSP頁(yè)面,3個(gè)。

index.jsp

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

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

  • <html>

  • <head>

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

  • </head>

  • <body>

  • <h4>請(qǐng)支付:</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>重復(fù)提交了</title>

  • </head>

  • <body>

  • <h2>系統(tǒng)正在處理,請(qǐng)勿重復(fù)提交!</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 {

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

  • privatestaticfinal String TOKEN = "TOKEN";

  • /**

  • ? ? * 獲得唯一的token

  • ? ? * @param request

  • ? ? * @return

  • ? ? */

  • public String getToken(HttpServletRequest request) {

  • //★UUID可以產(chǎn)生唯一的序列碼

  • UUID uuid = UUID.randomUUID();

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

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

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

  • return token;

  • ? ?}

  • /**

  • ? ? * 驗(yàn)證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);

  • ? ? ? ?}

  • ? ?}

  • /**

  • ? ? * 產(chǎn)生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

    昨天和我們的架構(gòu)聊到這個(gè)問(wèn)題,因?yàn)楝F(xiàn)在做的項(xiàng)目涉及到了。他說(shuō):對(duì)于分布式的大型互聯(lián)網(wǎng)項(xiàng)目,這種token的方式,在高并發(fā)時(shí)可能會(huì)出現(xiàn)問(wèn)題。所以,也建議用redirect最保險(xiǎn)吧。謝謝













    轉(zhuǎn)載于:https://blog.51cto.com/hanchaohan/931145

    總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。