<% @ 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 ");
Rand = Rand. substring (0, Rand. indexof ("."));
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 );
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 ());
%>