JSP page Verification Code Generation

Source: Internet
Author: User

<% @ Page contenttype = "image/JPEG" Import = "Java. AWT .*,
Java. AWT. image. *, java. util. *, javax. ImageIO. * "%>
<%
// Create an image in memory
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );

// Obtain the image Context
Graphics G = image. getgraphics ();

// Set the background color
G. setcolor (new color (0 xdcdcdc ));
G. fillrect (0, 0, width, height );

// Draw a border
G. setcolor (color. Black );
G. drawrect (0, 0, width-1, height-1 );

// Obtain the random ID code (4 digits)
// String Rand = request. getparameter ("RAND ");
Random ran = new random ();
String Rand = string. valueof (ran. nextint (9999 ));

Switch (RAND. Length ())
{
Case 1: Rand = "000" + rand; break;
Case 2: Rand = "00" + rand; break;
Case 3: Rand = "0" + rand; break;
Default: Rand = Rand. substring (0, 4); break;
}

// Save the authentication code to the session
Session. setattribute ("RAND", RAND );

// Display the authentication code to the image
G. setcolor (color. Black );
G. setfont (new font ("Atlanta inline", Font. Plain, 18 ));
String STR = Rand. substring (0, 1 );
G. drawstring (STR, 8, 17 );

STR = Rand. substring (1, 2 );
G. drawstring (STR, 20, 15 );
STR = Rand. substring (2, 3 );
G. drawstring (STR, 35, 18 );

STR = Rand. substring (3, 4 );
G. drawstring (STR, 45, 15 );

// 88 random interference points are generated, making the identification code in the image hard to beProgramDetected
Random random = new random ();
For (INT I = 0; I <88; I ++)
{
Int x = random. nextint (width );
Int y = random. nextint (height );
G. drawoval (X, Y, 0, 0 );
}

// The image takes effect
G. Dispose ();

// Output the image to the page
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
%>

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.