Java ways to prevent repeated submission of Web Forms _java

Source: Internet
Author: User
Tags java web

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.