1.在我们的web开发中,常常会遇到表单的重复提交问题,那么我们的解决方案有两种:①重定向:response.sendrediect(); ②token的使用,即做个标记


下面写一个token的例子:(在tomcat上可以运行的。)

1.JSP页面,3个。

index.jsp

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

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

  3. <html>

  4. <head>

  5. <title>避免表单重复提交的例子</title>

  6. </head>

  7. <body>

  8. <h4>请支付:</h4>

  9. <%

  10.    String token = (String)request.getAttribute("mytoken");

  11.        out.print(token);

  12.    %>

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

  14. <inputtype="hidden"name="token"value="<%=token %>"/>

  15. <inputtype="text"name="money"/>

  16. <inputtype="submit"value="支付"/>

  17. </form>

  18. </body>

  19. </html>

suc.jsp

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

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

  3. <html>

  4. <head>

  5. <title>支付成功</title>

  6. </head>

  7. <body>

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

  9. </body>

  10. </html>

error.jsp

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

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

  3. <html>

  4. <head>

  5. <title>重复提交了</title>

  6. </head>

  7. <body>

  8. <h2>系统正在处理,请勿重复提交!</h2>

  9. </body>

  10. </html>

2.com.hanchao.web.util是我们工具包

 
  
  1. package com.hanchao.web.util;

  2. import java.util.UUID;

  3. import javax.servlet.http.HttpServletRequest;

  4. import javax.servlet.http.HttpSession;

  5. /**

  6. * 工具类

  7. * @author hanlw

  8. * 2012-07-09

  9. */

  10. publicclass TokenUtil {

  11. //定义一个常量,此常量只是为以后取值,传值方便

  12. privatestaticfinal String TOKEN = "TOKEN";

  13. /**

  14.     * 获得唯一的token

  15.     * @param request

  16.     * @return

  17.     */

  18. public String getToken(HttpServletRequest request) {

  19. //★UUID可以产生唯一的序列码

  20. UUID uuid = UUID.randomUUID();

  21.        String token = uuid.toString();

  22.        HttpSession session = request.getSession();

  23.        session.setAttribute(TOKEN, token);

  24. return token;

  25.    }

  26. /**

  27.     * 验证token

  28.     * @param request

  29.     * @param requestToken

  30.     * @return

  31.     */

  32. publicboolean validateToken(HttpServletRequest request,String requestToken) {

  33.        HttpSession session = request.getSession();

  34.        String sessionToken = (String) session.getAttribute(TOKEN);

  35. if(sessionToken != null &&

  36.           requestToken != null &&

  37.           sessionToken.equals(requestToken)) {

  38.            session.removeAttribute(TOKEN);

  39. returntrue;

  40.        } else {

  41. returnfalse;

  42.        }

  43.    }

  44. }

3.我们的servlet

 
  
  1. package com.hanchao.web.util;

  2. import java.io.IOException;

  3. import javax.servlet.ServletException;

  4. import javax.servlet.http.HttpServlet;

  5. import javax.servlet.http.HttpServletRequest;

  6. import javax.servlet.http.HttpServletResponse;

  7. publicclass TokenServlet extends HttpServlet {

  8. privatestaticfinallong serialVersionUID = 1L;

  9. publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

  10. throws ServletException, IOException {

  11.        String method = request.getParameter("_m");

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

  13.            pay(request,response);

  14.        } elseif("tosave".equals(method)) {

  15.            save(request,response);

  16.        }

  17.    }

  18. /**

  19.     * 产生token

  20.     * @param request

  21.     * @param response

  22.     * @throws ServletException

  23.     * @throws IOException

  24.     */

  25. privatevoid save(HttpServletRequest request, HttpServletResponse response)  

  26. throws ServletException, IOException{

  27.        TokenUtil util = new TokenUtil();

  28.        String token = util.getToken(request);

  29.        request.setAttribute("mytoken", token);

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

  31.    }

  32. /**

  33.     * 支付

  34.     * @param request

  35.     * @param response

  36.     * @throws ServletException

  37.     * @throws IOException

  38.     */

  39. privatevoid pay(HttpServletRequest request, HttpServletResponse response)

  40. throws ServletException, IOException{

  41.        String token = request.getParameter("token");

  42.        TokenUtil util = new TokenUtil();

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

  44. if(result) {

  45.            String money = request.getParameter("money");

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

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

  48.        } else {

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

  50.        }

  51.    }

  52. publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

  53. throws ServletException, IOException {

  54.        doGet(request,response);

  55.    }

  56. }



==================================================================

2013-06-21-add-han

昨天和我们的架构聊到这个问题,因为现在做的项目涉及到了。他说:对于分布式的大型互联网项目,这种token的方式,在高并发时可能会出现问题。所以,也建议用redirect最保险吧。谢谢