How to generate an image verification code with servletjsp

xiaoxiao2021-03-05  26

Now there are many websites when the user fills in the form, and the value of the verification code is required to fill in the verification code. The purpose of the verification code is to prevent all the web downloads of the website through traversal links. It is also possible to prevent users from using the resources of the website without this website. So now there are a lot of websites that use verification code technology. The verification code is usually a random string generated on a web server, saved in some way, such as saving to the current session, then submit webpages in the user Whether the verification input to the user is consistent, however, if it is directly in a clear text, it is still unable to prevent some software that automatically fill the form. Therefore, the verification code is generally displayed in the form of a picture, and it can be processed in the character string displayed in the picture, such as using a rotary character, adding a background texture, and other technologies to increase the difficulty of software recognition. Let's take a brief introduction to this verification code:

First implement a servlet used to generate a picture (of course, you can also implement it with JSP):

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.io. *;

Import java.util. *;

Import com.sun.Image.codec.jpeg. *;

Import java.awt. *;

Import com.sun.Image.codec.jpeg. *;

Import java.awt.image.bufferedImage;

Import java.awt.image.database;

Import java.awt.geom.Generalpath;

Import javax.swing. *;

Import java.math. *;

Public Class Servlet1

Extends httpservlet {

// Process the http get request

Public void doget (httpservletRequest Request, HttpservletResponse Response) THROWS

ServletException, IOException {

Response.setContentType (Content_Type);

Response.setContentType ("image / jpeg"); // must set contentType as image / jpeg

INT length = 4; // Settings 4 numbers by default

Date D = new date ();

Long lseed = d.gettime ();

Java.util.random r = new random (LSEED); // Setting the random seed

IF (Request.getParameter ("Length")! = null) {

Try {

Length = integer.parseint (Request.GetParameter ("Length"));

}

Catch (NumberFormatexception E) {

}

}

StringBuffer str = new stringbuffer ();

For (INT i = 0; i

Str.Append (R.Nextint (9)); / / Generate random numbers

}

/ / Can be added to the code for saving the verification code here

// Create a memory image

BufferedImage Bi = New BufferedImage (40, 16, BufferedImage.Type_INT_RGB);

Graphics2d g = bi.creategraphics ();

G.clearRect (0, 0, 16, 40); g.setcolor (color.green.cyan);

g.drawstring (Str.Tostring (), 4, 12);

Try {

// Use JPEG encoding, output to Response output stream

JPEGIMAGEENCODER Encoder = JPEGCODEC.CREATEJPEGENCODER (response.

GetputStream ());

JPEgencodeparam param = encoder.getDefaultjpegencodeparam (BI);

Param.Setquality

1.0F, false);

Encoder.setjpegencodepAram (param);

Encoder.Encode (BI);

}

Catch (Exception EX) {

}

}

}

Then add the following code to display the verification code in the demand.

Replace / WebModule1 / Servlet1 with the full path to the servlet you use to generate the verification code.

转载请注明原文地址:https://www.9cbs.com/read-33732.html

New Post(0)