1 Packageservlet;2 ImportJava.awt.Color;3 ImportJava.awt.Font;4 ImportJava.awt.Graphics;5 ImportJava.awt.Graphics2D;6 ImportJava.awt.image.BufferedImage;7 Importjava.io.IOException;8 ImportJava.util.Random;9 ImportJavax.imageio.ImageIO;Ten Importjavax.servlet.ServletException; One ImportJavax.servlet.http.HttpServlet; A Importjavax.servlet.http.HttpServletRequest; - ImportJavax.servlet.http.HttpServletResponse; - /** the * Generate a picture in memory by Javaapi. - * Response to the client via the response output stream. - */ - Public classCheckcodeextendsHttpServlet { + Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { + //Disable Caching A //Response.setheader ("Cache-control", "No-cache"); at //Response.setheader ("Pragma", "No-cache"); - //Response.setdateheader ("Expires",-1); - intwidth = 120; - intHeight = 30; - //Step one draw an in-memory picture -BufferedImage BufferedImage =Newbufferedimage (width, height, in Bufferedimage.type_int_rgb); - //Step two picture draw background color---by drawing object toGraphics graphics = Bufferedimage.getgraphics ();//Get Paint Object---brush + //You must specify a color before drawing any graphics -Graphics.setcolor (Getrandcolor (200, 250)); theGraphics.fillrect (0, 0, width, height); * //step three Draw border $ Graphics.setcolor (color.white);Panax NotoginsengGraphics.drawrect (0, 0, width-1, height-1); - //Step 44 Random numbers theGraphics2D graphics2d =(graphics2d) graphics; + //Setting the output font AGraphics2d.setfont (NewFont ("Arial", Font.Bold, 18)); theString words = +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; -Random random =NewRandom ();//Generate random numbers $ //Save the randomly generated verification code to the session: $StringBuffer buffer =NewStringBuffer (); - //defining x-Coordinates - intx = 10; the for(inti = 0; I < 4; i++) { - //Random ColorWuyiGraphics2d.setcolor (NewColor (+ random.nextint (110), 20 +Random the. Nextint (+), + random.nextint (110))); - //Rotate -30---30 degrees Wu intJiaodu = Random.nextint (60)-30; - //Conversion radians About Doubletheta = Jiaodu * math.pi/180; $ //generate a random number - intindex = Random.nextint (Words.length ());//generate random number 0 to length-1 - //get alpha-numeric - Charc =Words.charat (index); A //Add the generated kanji to buffer + Buffer.append (c); the //output C to picture -Graphics2d.rotate (theta, X, 20); $Graphics2d.drawstring (String.valueof (c), X, 20); theGraphics2d.rotate (-theta, X, 20); thex + = 30; the } the //JIS1 Storage to session -Request.getsession (). SetAttribute ("code", buffer.tostring ()); inSystem.out.print ("stored in session. "); the System.out.println (buffer.tostring ()); the //Step five draw the interference line AboutGraphics.setcolor (Getrandcolor (160, 200)); the intX1; the intx2; the intY1; + intY2; - for(inti = 0; I < 30; i++) { theX1 =random.nextint (width);Bayix2 = Random.nextint (12); theY1 =random.nextint (height); they2 = Random.nextint (12); -Graphics.drawline (x1, y1, x1 + x2, x2 +y2); - } the //output the above image to the browser ImageIO theGraphics.dispose ();//Freeing Resources theImageio.write (bufferedimage, "JPG", Response.getoutputstream ()); the } - Public voidDoPost (httpservletrequest request, httpservletresponse response) the throwsservletexception, IOException { the doget (request, response); the }94 /** the * Take a range of color the * @paramFC the * int Range parameter 198 * @paramBC About * int Range parameter 2 - * @returnColor101 */102 PrivateColor Getrandcolor (intFcintBC) {103 //take its random color104Random random =NewRandom (); the if(FC > 255) {106FC = 255;107 }108 if(BC > 255) {109BC = 255; the }111 intr = FC + random.nextint (BC-FC); the intg = FC + random.nextint (BC-FC);113 intB = fc + random.nextint (BC-FC); the return NewColor (R, G, b); the } the}
Captcha HttpServlet