Num. jsp <% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%><%! Color getRandColor (IntCc,IntBb) { Random random =NewRandom (); If(Fc> 255) cc = 255; If(Bc> 255) bb = 255; IntR = cc + random. nextInt (bb-cc ); IntG = cc + random. nextInt (bb-cc ); IntB = cc + random. nextInt (bb-cc ); ReturnnewColor (r, g, B ); } // Obtain the random color
%> <% Response. setHeader ("Pragma", "No-cache "); Response. setHeader ("Cache-Control", "no-cache "); Response. setDateHeader ("Expires", 0 ); IntWidth = 80; // defines the length of the Verification Code image. IntHeight = 30; // defines the image width of the verification code. Bufferedimage image =NewBufferedimage (width, height, bufferedimage. type_int_rgb ); Graphics G = image. getgraphics (); Random random =NewRandom (); G. setcolor (getrandcolor (200,250 )); G. fillrect (0, 0, width, height ); G. setfont (NewFont ("Times New Roman", Font. Plain, 18 )); // Define the font format G. setcolor (getrandcolor (160,200 )); For(IntI = 0; I <155; I ++) { IntI _x = random. nextint (width ); IntI _y = random. nextInt (height ); IntI _xl = random. nextInt (12 ); IntI _yl = random. nextInt (12 ); G. drawLine (I _x, I _y, I _x + I _xl, I _y + I _yl ); } // Draw the background with a line bar
String s_Rand = ""; For(IntI = 0; I <4; I ++) { String rand = String. valueOf (random. nextInt (10 )); S_Rand + = rand; G. setColor (NewColor (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 ))); G. drawString (rand, 13 * I + 6, 16 ); } // Generates four random Codes Session. setAttribute ("rand", s_Rand ); // Save the verification code to the Session G. dispose (); ImageIO. write (image, "JPEG", response. getOutputStream ()); // Output verification Image
Out. clear (); Out = pageContext. pushBody (); %> |