欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA防止重复提交Web表单的方法

程序员文章站 2024-03-07 11:16:21
本文实例讲述了java防止重复提交web表单的方法。分享给大家供大家参考,具体如下: package cn.com.form; import java.io.i...

本文实例讲述了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);
    }
  }
}

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <base href="<%=basepath%>">
 <title>my jsp 'form.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0"> 
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
 </head>
 <body>
   <form action="/session/doform" method="post">
     <input type="hidden" name="token" value="${token}">
     用户名:<input type="text" name="username">
     <input type="submit" value="提交">
   </form>
 </body>
</html>

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程序设计有所帮助。