JSP Verification Code Daquan digital verification code (source code)

Source: Internet
Author: User

Verification codes are widely used in various scenarios, the random code function is added to the new user account registration, user logon, website unified login, or the user publishes an article or content in the website information module in a variety of systems or software, the verification code is used to prevent the abuse of automatically registered or published programs on the network. The principle of the Verification Code is to randomly select some verification codes to display the verification code in the form of images on the software verification interface or verification page, when submitting an image, you must enter and submit the verification code on the image. If the submitted verification code is the same as the verification code saved by the server session, the submitted information is considered valid, otherwise, submission is rejected. When using the verification code, in order to avoid the automatic analysis program to parse the image and obtain the verification information, it is usually necessary to randomly generate some interference lines on the image or distort and blur the image with complicated characters, this increases the difficulty of automatically identifying programs to analyze and verify images.

JSP verification code source code Daquan will be used to introduce the implementation of several verification codes in JSP source code and use.

I. source code of the digital verification code generated in JSP

A digital verification code is the most common form of verification Verification code. The following is the JSP source code for digital implementation:

Num. jsp
<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%>

<%!

Color getRandColor (IntCc,IntBb)

{

Random random =NewRandom ();

If(Fc> 255) cc = 255;

If(Bc> 255) bb = 255;

IntR = cc + random. nextInt (bb-cc );

IntG = cc + random. nextInt (bb-cc );

IntB = cc + random. nextInt (bb-cc );

ReturnnewColor (r, g, B );

} // Obtain the random color

%>

<%

Response. setHeader ("Pragma", "No-cache ");

Response. setHeader ("Cache-Control", "no-cache ");

Response. setDateHeader ("Expires", 0 );

IntWidth = 80; // defines the length of the Verification Code image.

IntHeight = 30; // defines the image width of the verification code.

Bufferedimage image =NewBufferedimage (width, height, bufferedimage. type_int_rgb );

Graphics G = image. getgraphics ();

Random random =NewRandom ();

G. setcolor (getrandcolor (200,250 ));

G. fillrect (0, 0, width, height );

G. setfont (NewFont ("Times New Roman", Font. Plain, 18 ));

// Define the font format

G. setcolor (getrandcolor (160,200 ));

For(IntI = 0; I <155; I ++)

{

IntI _x = random. nextint (width );

IntI _y = random. nextInt (height );

IntI _xl = random. nextInt (12 );

IntI _yl = random. nextInt (12 );

G. drawLine (I _x, I _y, I _x + I _xl, I _y + I _yl );

}

// Draw the background with a line bar

String s_Rand = "";

For(IntI = 0; I <4; I ++)

{

String rand = String. valueOf (random. nextInt (10 ));

S_Rand + = rand;

G. setColor (NewColor (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 )));

G. drawString (rand, 13 * I + 6, 16 );

}

// Generates four random Codes

Session. setAttribute ("rand", s_Rand );

// Save the verification code to the Session

G. dispose ();

ImageIO. write (image, "JPEG", response. getOutputStream ());

// Output verification Image

Out. clear ();

Out = pageContext. pushBody ();

%>

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.