<% @ Page ContentType = "Image / JPEG" Import = "java.awt. *, java.util. *, javax.imageio. *"%> <% // created in memory Image int width = 60, Height = 20; bufferedimage image = new bufferedimage (width, height, bufferedimage.type_int_rgb);
/ / Get graphics context graphics g = image.getgraphics ();
// Set the background color G.SetColor (New Color (0xDCDCDC)); G.FillRect (0, 0, Width, Height);
// Select Border G.SetColor (Color.Black); g.drawRect (0, 0, Width-1, Height-1);
// Take the randomly generated authentication 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; casse 2: rand = "00" rand; break; cas 3: rand = "0" rand; break; default: Rand = rand.substring (0, 4); Break;}
// Deposit the authentication code into session session.setttribute ("rand", rand);
// Display the authentication code to g.setcolor (color.ble); G.SetFont (New Font ("Atlantic 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);
// randomly generate 88 interference points, so that the authentication code in the image is not easy to detect 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);}
// Image take effect g.dispose ();
// Output image to page imageio.write (image, "jpeg", response.getOutputStream ());%>