Generate images with 4 random numbers and messy backgrounds, the numbers and background colors will change, the server is new (use history.go (-1) variable) prototype reference Alibaba
http://china.alibaba.com/member/showimage
------------ Generate a verification code picture ----- image.jsp ------------------------ ------------------- <% @ Page ContentType = "image / jpeg" import = "java.awt. *, Java.awt.image. *, Java.util * "%> <%! 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);}%> <% // Settings page does not cache Response.setHeader ("Pragma", "NO-Cache"); response.setheader ("cache-control", " "); response.setdateHeader (" expires ", 0); // Create image in memory int width = 60, Height = 20; bufferedimage image = new bufferedImage (width, height, bufferedimage.type_int_rgb); / / Get graphics context graphics g = image.getgraphics (); // Generate random class random random = new random (); // Set background color G.SetColor (GetrandColor (200, 25)); g.FillRect (0, 0, Width, height); // Set font G.SetFont (New Font ("Times New Roman", Font.Plain, 18)); // Section Box //g.SetColor (New Color ()); //g .drawRect (0, 0, Width-1, H EIGHT-1); // randomly generate 155 interferent lines, enabling the authentication code in the image 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 YL);} // Take the randomly generated authentication code (4 digits) SRAND = ""; for (int i = 0; i <4; i ) {string rand = string.valueof (Random .nextint (10)); SRAND = rand; // Display the authentication code to the image G.SetColor (New Color (20 random.nextint (110), 20 random.nextint (110), 20 random. Nextint (110)))));
// The color of the call is the same, it may be because the seed is too close, so only directly generate G.DrawString (Rand, 13 * i 6, 16);} // Deposit the authentication code in sessionSession.setttribute ("Rand) ", SRAND); // Image Effective g.dispose (); // Output Image to Page Imageio.write (Image," JPEG ", Response.getOutputStream ());%> -------- ------- Use the verification code picture --------- A.jsp ------------------------- ----------- <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> < HTML>