JAVAがWebフォームの重複提出を防止する方法

4584 ワード

この例では,JAVAが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;
//    
public class FormServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //     
    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.       
   * 2.      
   * 3.        ,           
   * */
  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);
    }
  }
}






 
 
 My JSP 'form.jsp' starting page
  
  
   
  
  
  
 
 
   
ユーザー名:

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("          ...");
    */
    boolean b=isTokenValid(request);
    if(!b)
    {
      System.out.println("       !");
      return;
    }
    request.getSession().removeAttribute("token");
    System.out.println("         ==");
  }
  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プログラムの設計に役立つことを望んでいます.