<% @ Page contenttype = "image / jpeg" import = "java.awt. *, Java.awt.image. *, Java.util. *, Javax.imageio. *"%> <%! Color getrandcolor (int FC) , int BC) {// given random color random random = new random (); if (fc> 255) fc = 255; if (bc> 255) BC = 255; int R = fc random.nextint BC-FC); int g = fc random.nextint (BC-FC); int b = fc random.nextint (BC-FC); Return New Color (R, G, B);}%> <% / / Set the page does not cache Response.setHeader ("prgma", "no-cache"); response.setheader ("cache-control", "no-cache"); response.setdateHeader ("expires", 0); // created in memory image int width = 60, height = 20; BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); // Get the graphics context graphics g = image.getGraphics (); // generate random class random Random = new random (); // Set background color G.SetColor (GetrandColor (200, 250)); g.FillRect (0, 0, width, height); // Set font G.SetFont (New Font " New Roman ", Font.Plain, 18)); // Sea Box G.SetColor (New Color (255, 255, 255)); g.drawRect (0, 0, Width-1, Height-1); // randomly generated 155 The interference line makes the authentication code in the image is not easy to detect G.SetColor (GetrandColor (160,200)); for (int i = 0; i <155; i ) {int x = random.nextint (width); Int Y = Random.nextint (Height); int xl = random.nextint (12); int yl = random.nextint (12); g.drawline (x, y, x xl, y );} // Take random Generated authentication code (4 digits) string srand = ""; for (int i = 0; i <4; i ) {string rand = string.valueof (Random.NextINT (10)); SRAND = rand; // Display the authentication code into the image G.SetColor (New Color (20 Random.Nextint (110), 20 Random.nextint (110))))); // Call the function The color is the same, it may be because the seed is too close, so only G.DrawString (Rand, 13 * i 6, 16) can only be generated;} // Deposit the authentication code in sessionSession.SetAttribute ("rand", SRAND); / / Image take effect g.dispose ();