JSP Verification Code

Source: Internet
Author: User

It is important to note that the second approach should be used under resin:

<% @pageImport= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*, Sun.misc.BASE64Encoder, Java.awt.image.DataBufferByte , Java.io.*,javax.swing.imageicon "%>//Tomcat under<%@ page contenttype= "image/jpg" language= "java"%>//Resin under<%@ page contenttype= "text/html" language= "java"%><%!Color Getrandcolor (intFcintBC) {//get random colors for a given rangeRandom random =NewRandom (); if(fc>255) fc=255; if(bc>255) bc=255; intR=fc+random.nextint (bc-FC); intG=fc+random.nextint (bc-FC); intB=fc+random.nextint (bc-FC); return NewColor (r,g,b);}%><%out.clear ();//Settings page does not cacheResponse.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0);//Create an image in memoryintWIDTH=60, height=30; BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb);//Get the graphics contextGraphics g =image.getgraphics ();//Generating Random classesRandom random =NewRandom ();//Set Background colorG.setcolor (Getrandcolor (200,250)); G.fillrect (0, 0, width, height);//Set FontG.setfont (NewFont ("Times New Roman", Font.plain, 32));//Draw Border//G.setcolor (New Color ());//G.drawrect (0,0,width-1,height-1);//randomly generates 155 lines of interference so that the authentication code in the image is not easily detected by other programsG.setcolor (Getrandcolor (160,200)); for(inti=0;i<155;i++) {        intx =random.nextint (width); inty =random.nextint (height); intXL = Random.nextint (12); intYL = Random.nextint (12); G.drawline (X,y,x+xl,y+yl);}//randomly generated authentication code (4 digits)/*for (int i=0; i < srand.length (); i++) {String rand=string.valueof (Srand.charat (i)); Display the authentication code to the image in G.setcolor (new Color (20+random.nextint), 20+random.nextint (20+random.nextint (110)));// The calling function comes out with the same color, possibly because the seed is too close, so only g.drawstring (rand, 13*i + 6, +) can be generated directly;}*/String SRand= ""; for(inti=0;i<4;i++) {String rand=string.valueof (Random.nextint (10)); SRand+=Rand; //Display the authentication code to the imageG.setcolor (NewColor (20+random.nextint, 20+random.nextint (+), 20+random.nextint (110)));//The call function comes out with the same color, possibly because the seed is too close, so it can only be generated directlyg.DrawString (Rand, 13*i+2, 24);}//Save authentication Code to sessionSession.setattribute ("Validatecode", SRand);//Image Effectiveg.dispose ();//use the following method to output image to page under Tomcat//Imageio.write (image, "JPG", Response.getoutputstream ());//base64 solution under resinBytearrayoutputstream byteout =NewBytearrayoutputstream ();Try{imageio.write (image,"JPEG", byteout); byte[] data =Byteout.tobytearray (); Base64encoder Encoder=NewBase64encoder ();    Out.write (Encoder.encode (data));    Out.flush (); Out.clear ();} Catch(IOException e) {e.printstacktrace ();}%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.