Java Implementation Verification Code function

Source: Internet
Author: User

 PackageCom.verification_code.servlet;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classImgservletextendshttpservlet{@Overrideprotected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO auto-generated Method StubDoPost (REQ,RESP); } @Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO auto-generated Method Stub//set the response startResp.setcontenttype ("Image/jpeg"); //Settings page does not cacheResp.setheader ("Pragma", "No-cache"); Resp.setheader ("Cache-control", "No-cache"); Resp.setdateheader ("Expires", 0); OutputStream out=Resp.getoutputstream (); //Set the width height of the picture        intWidth=88,height=20; BufferedImage Image=NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); Graphics g=Image.getgraphics (); //Generating Random classesRandom random =NewRandom ();        G.setcolor (Color.White); G.fillrect (0, 0, width, height); //Set FontG.setfont (NewFont ("Times New Roman", font.bold+font.italic,18)); //Draw BorderG.setcolor (Getrandcolor (0, 20));//---2G.drawrect (0, 0, width-1, height-1); //randomly generated interference lines        intCount = 100; intLineWidth = 3;//width of the interference line         for(inti = 0; I < count; i++) {G.setcolor (Getrandcolor (150, 200));//---3            intx = Random.nextint (width-linewidth-1) + 1;//guaranteed to draw within the frame            inty = Random.nextint (height-linewidth-1) + 1; intXL =Random.nextint (linewidth); intYL =Random.nextint (linewidth); G.drawline (x, y, x+ XL, Y +yl); }        //generate random Verification codeString srand= ""; String str[]={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};  for(inti=0;i<4;i++){            intRand = Random.nextint (36); SRand+=Str[rand]; //Set Random ColorsG.setcolor (NewColor (20+random.nextint, 20+random.nextint), 20+random.nextint (110))); //Drawingg.DrawString (Str[rand], 20*i+8, 16); } req.getsession (). SetAttribute ("Verification_code", SRand);        G.dispose (); Imageio.write (Image,"JPEG", out); }     PublicColor Getrandcolor (intFcintBC) {//get random color 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); }}

Page

<script type= "text/javascript" >    function Refresh () {        document.getElementById ("Id_code" ). src= "Image?" +math.random ();         // loginform.code.src= "image?" +math.random ();    }
</script>

Java Implementation Verification Code function

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.