The example in this article describes how Java prevents repeated submission of Web Forms. Share to everyone for your reference, specific as follows:
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;
Generate form public class Formservlet extends HttpServlet {private static final long serialversionuid = 1L;
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Generating random number 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//Token{/* * 1. Make the constructor private * 2. Create yourself A * 3. Exposing a method that allows you to obtain the object created above * * * */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 encoding 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" >
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 * Request to process form submission */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 ("Submit registered users to the database ...");
* * Boolean b=istokenvalid (Request); if (!b) {System.out.println ("Please do not repeat the submission!")
");
Return
} request.getsession (). RemoveAttribute ("token");
SYSTEM.OUT.PRINTLN ("Register user = = in database"); 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);
}
}
I hope this article will help you with your Java Web programming.