Project module — 1. Implementation of verification code function



In the Java Web project, a verification code is randomly generated at the back end, drawn into an image, and two interference lines are added to the image and sent to the browser for the user to use.

The content of this blog includes the logical steps of function implementation, Java implementation code, and the generated verification code picture display.

Step 1: generate a four character string

Use an array char [] + a random object to implement this function. The code is as follows:

private char code[]={        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',        'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',        'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',        'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',        '3', '4', '5', '6', '7', '8', '9' };Random rd = new Random();String result = "";for(int i = 0;i < 4;i++ ){  result+=code[rd.nextInt[code.length]];}

Step 2: draw a picture

Use a BufferdImage object, Graphics object, Font object

Bufferedimage class: an image class whose generated image occupies a cache area in memory.

There are two ways to use it:

1. Read an existing picture file into the buffer of bufferedimage object, and then operate on the picture file.

  BufferedImage b = FileOutputStream(File f));

2. Create a bufferedimage object and draw it through the graphics object.

Bufferedimage B = new bufferedimage (int width, int height, indexcolormodel CN); / / set the width, height, and color setting modes of the picture

Graphics g = b.getgraphics(); / / get the picture plotter

3. Common methods of graphics objects

  g.setColor(Color color);

g. FillRect (int x, int y, int width, int height) / / two groups. Set the color for the specified position of the picture. Take the lower left corner of the picture as the origin, and (x, y) as the lower left corner of the picture

//Angular coordinates, the drawing area is width, height.

g. Drawstring (string s, int x, int y) / / draws a string in the (x, y) coordinate system. The origin of the coordinate system is the lower left corner of bufferedimage

g. DrawLine (int x1, int Y1, int X2, int Y2) / / draws a line between two points. The color is set by the nearest g.setcolor().

g. Dispose() / / release the system resources in use

BufferdImage bufferedImage = new     BufferedImage(50,30,BufferedImage.TYPE_INT_BGR);
 Graphics graphics = bufferedImage.getGraphics();
 Font font = new Font("Arial",Font.TRUETYPE_FONT,18);
 //Set background color and fill
 graphics.setColor(new Color(rd.nextInt(55)+200,rd.nextInt(55)+20,rd.nextInt(55)+200));
 Graphics. FillRect (0,0, width, height); // set the coordinates (x, y), width and height of the rectangle to be filled
 //Border color
 //Set font
 for (int i = 0; i

Step 3: send the picture to the browser to store the string in the generated verification code

Setting browser side can’t cache captcha picture

Pragma domain name: used to contain implementation specific instructions. Pragma: no cache

Cache controller domain name: used in http: 1.1 to specify caching rules.

Response.setheader ("pragma", "no cache"); // notify the browser not to use the cache function
        Response. Setheader ("cache control", "no cache"); // notify the browser not to cache

Set the media type of the response


Send the string in the verification code to the session for obtaining from the session during verification


Send the captcha picture to the browser

      OutputStream os = reponse.getOutputStream();
 }catch(IOException e){

Imageio class:

Image reading and writing tool class, which provides static methods to read pictures of any format into memory or write them to files.


Recommended Today

How many steps does it take from symfony framework to a complete project

preface aboutphpThe framework of bothyii,symfonyOr maybelaravelWe all dabble in our work. For the resource package stored in the frameworkvendorFolders, entry files(index.phpperhapsapp.php)And we all meet with them every day. But are you really familiar with these files / folders? How does a complete project develop from a pure framework? What role does each part play in […]