How to generate and use a graphic verification code for Java Web development, javaweb

Source: Internet
Author: User

How to generate and use a graphic verification code for Java Web development, javaweb

This document describes how to generate and use a graphic verification code for Java Web development. Share it with you for your reference. The details are as follows:

The graphic Verification Code is designed to enhance security and increase the difficulty of cracking passwords by traversing all possibilities.

The graphic Verification Code includes the following three parts:

① Generate a graphic verification code;
② Use on the page;
③ Verification;

1. Graphic Verification Code Generation

Assume that the graphic verification code is generated in Servlet, and the basic process generated in JavaBean or JSP is the same. The design process is as follows:

① Set the document type of the response;
② Generate random codes;
③ Save the random code to the session;
④ Generate an image;
⑤ Draw random code to the memory image;
6. Send memory images to the client;

1.1 set the document type of the response.

When responding to a user, you need to set the document type. to generate an image document type, you can set it to: image/gif.
In Servlet, the setting method is response. setContentType ("image/gif ");
For JSP pages, use: <% @ page contentType = "image/gif" %>

1.2 generate random code

You can generate random numbers based on various random generation policies, and set the character composition and length of random numbers.

The random character given in this article is all letters and numbers. The Random code generation policy is provided by the Random object. The reference code is as follows:

Random Character List:

public static final char[] code = {'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',  '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'};

Code for generating random codes:

StringBuffer checkcode = new StringBuffer (); // generates a for (int I = 0; I <code_length; I ++) for each loop) {int generated = (new Random ()). nextInt (62); checkcode. append (code [generated]);}

1.3 Save the random code to the session

To verify the verification code after the user submits the verification code, the generated verification code must be saved in the session. In Servlet, You need to obtain the session object before using it.

The following is the reference code:

// Save the generated verification code to the session. HttpSession session = request. getSession (true); session. setAttribute ("checkCode", checkcode. toString ());

1.4 generate images

Create an object using the BufferedImage class, and then draw a picture using the Drawing Object. The following is the reference code:

// Create a memory image. The parameter is the image size and type BufferedImage image = new BufferedImage (, BufferedImage. TYPE_INT_RGB); // obtain the Graphics handle Graphics g = image. getGraphics (); // set the paint brush color // g. setColor (Color. yellow); // draw the background g. fillRect (1.5,); display the random code on the image. refer to the following code: // set the font color g. setColor (Color. black); // draw the Verification Code g. drawString (checkcode. toString (),); // The image takes effect g. dispose ();

1.6 send the generated image to the client

The reference code is as follows:

Copy codeThe Code is as follows: ImageIO. write (image, "JPEG", response. getOutputStream ());
In this way, the dynamic graphic verification code is generated.

2. Use the graphic code on the page:

Use Tag. If the url-pattern value of the Servlet that generates the image is checkcode, the code for loading the image on the page is as follows:
Copy codeThe Code is as follows:
3. Verification

Obtain the verification code entered by the user, then obtain the saved verification code from the session, and compare it to determine whether it is the same, so as to complete the verification.

I hope this article will help you with JSP program design.

Related Article

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.