<% @ 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 ());
%>