Java Random verification code

Source: Internet
Author: User

The random code function added to many system registration, logon, or publishing information modules is to avoid the use of automatic registration or publishing programs.
The verification code is to randomly select some characters to display on the page in the form of images. If you submit a verification code, you must submit the characters on the image at the same time, if the submitted characters are different from those saved by the server session, the submitted information is considered invalid. In order to avoid automatic program analysis and resolution of images, it is usually possible to randomly generate some interference lines on the images or distort the characters to increase the difficulty.

We can use servlet to implement Random verification codes.

         ValidateCodeServlet     serialVersionUID = 1L      width=60      height=20      codeCount=4  x=0      [] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J''K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W''X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'      init()     String strWidth=.getInitParameter("width"    String strHeight=.getInitParameter("height"    String strCodeCount=.getInitParameter("codeCount"    (strWidth!= && strWidth.length()!=0=(strHeight!= && strHeight.length()!=0=(strCodeCount!= && strCodeCount.length()!=0==width/(codeCount+1=height-2=height-4     BufferedImage buffImg = =    Random random = 0, 0    Font font =  Font("Fixedsys"0, 0, width - 1, height - 1( i = 0; i < 160; i++ x = y = xl = random.nextInt(12 yl = random.nextInt(12+ xl, y +    StringBuffer randomCode =  red = 0, green = 0, blue = 0     ( i = 0; i < codeCount; i++    String strRand = String.valueOf(codeSequence[random.nextInt(36    red = random.nextInt(255= random.nextInt(255= random.nextInt(255    g.setColor(+ 1) *    HttpSession session ="validateCode"    resp.setHeader("Pragma", "no-cache""Cache-Control", "no-cache""Expires", 0"image/jpeg"    ServletOutputStream sos ="jpeg"

 

Servlet needs to be declared in web. xml

      ValidateCodeServlet    com.servlet.ValidateCodeServlet        width    200            height    80            codeCount    5                ValidateCodeServlet    /validateCodeServlet    

 

 

 

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.