Generate images with 4 random numbers and messy backgrounds, numbers and background colors will change, the server is refreshing (with history.go (-1) varies)
Prototype Reference Alibaba http://china.alibaba.com/member/showimage Generates the file image.jsp
<% @ 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);
// 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 random random = new random ();
// Set the background color G.SetColor (GetrandColor (200, 250)); G.FillRect (0, 0, Width, Height);
// Set the font G.SetFont (New Font ("Times New Roman", Font.Plain, 18));
// Sea Border //g.SetColor (New Color ()); // g.drawRect (0, 0, Width-1, Height-1);
// Randomly generate 155 interference lines, enabling the authentication code in the image to detect G.SetColor (GetrandColor (160, 200)) in the image; for (int = 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 function 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 into sessionSession.setttribute ("rand", SRAND);
// Image take effect g.dispose ();
// Output image to page imageio.write (image, "jpeg", response.getOutputStream ());%>
File A.jsp using the verification code picture
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%>