首页 > 代码库 > 利用Session防止表单重复提交

利用Session防止表单重复提交

在服务器端生成一个唯一的随机标识号,专业术语称为Token(令牌),同时在当前用户的Session域中保存这个Token。然后将Token发送到客户端的Form表单中,在Form表单中使用隐藏域来存储这个Token,表单提交的时候连同这个Token一起提交到服务器端,然后在服务器端判断客户端提交上来的Token与服务器端生成的Token是否一致,如果不一致,那就是重复提交了,此时服务器端就可以不处理重复提交的表单。如果相同则处理表单提交,处理完后清除当前用户的Session域中存储的标识号。
  在下列情况下,服务器程序将拒绝处理用户提交的表单请求:

    1. 存储Session域中的Token(令牌)与表单提交的Token(令牌)不同。
    2. 当前用户的Session中不存在Token(令牌)
    3. 用户提交的表单数据中没有Token(令牌)

 

生成Token的工具类TokenProccessor

package utils;

import java.util.Random;
import org.apache.commons.codec.binary.Base64;

public class TokenProccessor {
    private TokenProccessor(){
        
    }
    private static final TokenProccessor instance=new TokenProccessor();
    public static TokenProccessor getInstance(){
        return instance;
    }
    public String makeToken(){
        //Apache Commons Codec有提供Base64的编码与解码功能
        String token=(System.nanoTime()+new Random().nextInt(999999999))+"";
        byte[] bytes=token.getBytes();
        Base64 base64=new Base64();
        String s=base64.encodeToString(bytes);
        System.out.println(s);
        return s;    
    }
//    public static void main(String[] args){
//        System.out.println(TokenProccessor.getInstance().makeToken());
//    }
}

创建Servlet,用于生成Token(令牌)和跳转到form.jsp页面

package lala;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.TokenProccessor;

public class demo5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String token=TokenProccessor.getInstance().makeToken();
        request.getSession().setAttribute("token", token);
        request.getRequestDispatcher("/form.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);

    }

}

在form.jsp中使用隐藏域来存储Token(令牌)

<html>
  <head>   
    <title>My JSP ‘form.jsp‘ starting page</title>
  </head>
  
  <body>
  "${pageContext.request.contextPath }"
       <form action="${pageContext.request.contextPath }/demo6" method="post">
           <input type="hidden" name="token" value="http://www.mamicode.com/${token }">
           username:<input type="text" name="username"><br/>
           <input type="submit" value="http://www.mamicode.com/submit">
       </form>
  </body>
</html>

处理表单提交

package lala;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class demo6 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        boolean b=isRepeatSubmit(request);
        if(b==true){
            response.getWriter().write("不要重复提交");
            return;
        }
        request.getSession().removeAttribute("token");
        response.getWriter().write("已提交");
    }

    private boolean isRepeatSubmit(HttpServletRequest request) {
        String client_token=request.getParameter("token");
        if(client_token==null){
            return true;
        }
        String server_token=(String)request.getSession().getAttribute("token");
        if(server_token==null){
            return true;
        }
        if(!client_token.equals(server_token)){
            return true;
        }
        return false;
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);

    }

}

 

利用Session防止表单重复提交