Get the verification code from the server

Source: Internet
Author: User

1. Verification Code Generation Program

Writes the resulting picture to the output stream of the response

Imageio.write (bufferedimage, "JPG", Response.getoutputstream ());
 Packagecom.hdh.content;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Verification Code Generator * * **/ Public classCheckimgservletextendsHttpServlet {//Save all idioms in the collection    Privatelist<string> words =NewArraylist<string>(); @Override Public voidInit ()throwsservletexception {//initialization phase, read New_words.txt//The absolute disk path must be used to read files in the Web projectString path = Getservletcontext (). Getrealpath ("/web-inf/new_words.txt"); Try{BufferedReader Reader=NewBufferedReader (NewFileReader (path));            String Line;  while(line = Reader.readline ())! =NULL) {Words.add (line);        } reader.close (); } Catch(IOException e) {e.printstacktrace (); }    }     Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Disable Caching//Response.setheader ("Cache-control", "No-cache"); //Response.setheader ("Pragma", "No-cache"); //Response.setdateheader ("Expires",-1);        intwidth = 120; intHeight = 30; //Step one draw an in-memory pictureBufferedImage BufferedImage =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //Step two picture draw background color---by drawing objectGraphics graphics = Bufferedimage.getgraphics ();//Get Paint Object---brush//You must specify a color before drawing any graphicsGraphics.setcolor (Getrandcolor (200, 250)); Graphics.fillrect (0, 0, width, height); //step three Draw borderGraphics.setcolor (Color.White); Graphics.drawrect (0, 0, width-1, height-1); //Step 44 Random numbersGraphics2D graphics2d =(graphics2d) graphics; //Setting the output fontGraphics2d.setfont (NewFont ("Arial", Font.Bold, 18)); Random Random=NewRandom ();//Generate random numbers        intindex =Random.nextint (Words.size ()); String Word= Words.get (index);//Get Idioms//defining x-Coordinates        intx = 10;  for(inti = 0; I < word.length (); i++) {            //Random ColorGraphics2d.setcolor (NewColor (+ random.nextint (110), 20 +random. Nextint (), + random.nextint (110))); //Rotate -30---30 degrees            intJiaodu = Random.nextint (60)-30; //Conversion radians            Doubletheta = Jiaodu * math.pi/180; //get alpha-numeric            Charc =Word.charat (i); //output C to pictureGraphics2d.rotate (theta, X, 20); Graphics2d.drawstring (String.valueof (c), X,20); Graphics2d.rotate (-theta, X, 20); X+ = 30; }        //save Captcha content to sessionRequest.getsession (). SetAttribute ("Checkcode_session", Word); //Step five draw the interference lineGraphics.setcolor (Getrandcolor (160, 200)); intX1; intx2; intY1; intY2;  for(inti = 0; I < 30; i++) {x1=random.nextint (width); X2= Random.nextint (12); Y1=random.nextint (height); Y2= Random.nextint (12); Graphics.drawline (x1, y1, x1+ x2, x2 +y2); }        //output the above image to the browser ImageIOGraphics.dispose ();//Freeing Resources//write the picture in Response.getoutputstream ()Imageio.write (bufferedimage, "JPG", Response.getoutputstream ()); }     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }    /*** Take a range of color * *@paramFC * int Range parameter 1 *@paramBC * INT Range Parameter 2 *@returnColor*/    PrivateColor Getrandcolor (intFcintBC) {        //take its random colorRandom 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); }}

2. To a tag through the time parameter in JS when the single-machine verification code constantly refresh the address

<! DOCTYPE html>    function checkimg (obj) {        obj.src= "/web14/checkimg?time" + New Date (). GetTime ();    } </script>        User name: <input type= "text" name= "username" ><br>          Password:<input type= "text" name= "password" >< Br>          Verification Code:<input type= "text" name= "username" > <br>         <input type=" Submit "value=" sign in "><br>    </form></body> 

3. Running Results

Get the verification code from the server

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.