The main purpose of graphic verification codes is to enhance security,It makes it harder for users to crack passwords by traversing all possibilities.

The use of graphic verification codes includes the following 3 parts:

① Graphic verification code generation;

② Use in pages;

③ verification;

1. Graphic verification code generation

Let's say that a graphic verification code is generated in the servlet,The basic process generated in javabean or jsp is the same.Design the following process:

① Set the document type of the response;

② Generate random code;

③ Save the random code in the session;

④ Generate pictures;

⑤ Draw random code on memory picture;

⑥ Send the memory image to the client;

1.1 Set the response document type

When responding to users,Need to set the document type,To generate a picture document type can be set to:image/gif.

The setting method in the servlet is:response.setcontenttype ("image/gif");

If used in a jsp page, you need to use:<%@page contenttype="image/gif"%>

1.2 Generate Random Code

Can be generated according to various random number generation strategies,And you can set which characters the random code consists of and the length of the random code.

The random code characters given in this article are all letters and numbers.The random code generation strategy used is provided by the random object.The reference code is as follows:

Random code 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 code:

stringbuffer checkcode=new stringbuffer ();
//every cycle,Generate a bit
for (int i=0;i<code_length;i ++)
  int generated=(new random ()). nextint (62);
  checkcode.append (code [generated]);

1.3 Store random code in session

In order to verify after a user submits a verification code,Need to save the generated verification code,Can be saved in the session. In the servlet, you need to get the session object before using it.

Here 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 Image

Create objects using the bufferedimage class,Then use the paint object to draw.Here is the reference code:

//create memory image,The parameters are the size and type of the picture.
bufferedimage image=new bufferedimage (49,14, bufferedimage.type_int_rgb);
//get graphics handle
graphics g=image.getgraphics ();
//Set the brush color
//g.setcolor (color.yellow);
//draw the background
g.fillrect (0,1,49,12);
1.5 Display the random code on the picture
Consider the following code:
//Set font color
g.setcolor (color.black);
//draw verification code
g.drawstring (checkcode.tostring (), 4,11);
//image takes effect
g.dispose ();

1.6 Send the generated image to the client

The reference code is as follows:

imageio.write (image, "jpeg", response.getoutputstream ());

This completes the generation of the dynamic graphic verification code.

2. Use graphic code in the page:

In the same way as other graphics,Use thetag. Assuming the url-pattern value of the servlet that generates the image is checkcode, the code for loading the image in the page is as follows:

<img border=0 src="checkcode">

3. Verification

Get the verification code entered by the user,Then get the saved verification code from the session,Comparison to determine if they are the same,This completes the verification.

  • Previous Example of Android phone battery power usage
  • Next How to solve the multi-field value problem when adding ASPNET