JSP Verification Code routine

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 r = new random ();
String Rand = (1000 + R. nextint (9999) + "";

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

// Display the authentication code to the image
G. setcolor (color. Black );
Integer tempnumber = new INTEGER (RAND );
String numberstr = tempnumber. tostring ();

G. setfont (new font ("Atlanta inline", Font. Plain, 18 ));
String STR = numberstr. substring (0, 1 );
G. drawstring (STR, 8, 17 );

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

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

// 88 random interference points are generated, making the authentication code in the image hard to be detected by other programs.
Random random = new random ();
For (INT I = 0; I <20; 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.