Java Code Verification Code

Source: Internet
Author: User

Java Background Code (CHECKCODESERVLET.JAVA)

 Packageweb;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCheckcodeservletextendsHttpServlet {/** Return a live picture **/     Public voidService (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/jpeg"); //Get byte streamOutputStream os=Response.getoutputstream (); //image Map Area, understand the board on the line, draw verification code//bufferedimage image=new bufferedimage (width,height,imagetype);BufferedImage image=NewBufferedImage (80,40, Bufferedimage.type_int_rgb); //BrushesGraphics g=Image.getgraphics (); //Add background color (random)Random r=NewRandom (); G.setcolor (NewColor (R.nextint (R.nextint), R.nextint (256));//color (red,green,blue); RGB format Colors SettingsG.fillrect (0, 0, 80, 40);//draw a rectangular area//Draw a StringG.setcolor (Color.Black);//set the color of the verification codeG.setfont (NewFont (NULL, font.bold,20));//Set the font fonts (font, style, size) of the captchaString Num=getnum (5);//Incoming Verification Codeg.DrawString (NUM, 5, 30);//(string content, x-coordinate, y-coordinate)//scribing, interfering with the robot identification verification Code         for(inti=0;i<6;i++){            //Draw 6 LinesG.setcolor (NewColor (R.nextint (R.nextint), R.nextint (256));//set the color of a lineG.drawline (R.nextint), R.nextint, R.nextint (+), R.nextint (80));//G.drawline (start x coordinate, start y coordinate, end x coordinate, end point y coordinate);        }        //compressed transmission (the picture is larger, need to compress, draw a 24-bit graph)Imageio.write (image, "JPEG", OS); }    //return the correct verification code//Length: Number of verification codes     PublicString Getnum (intlength) {String str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//constants, in order to pick out random verification codes based on lengthRandom r=NewRandom (); StringBuffer SF=NewStringBuffer ();  for(inti=0;i<length;i++){            intIndex=r.nextint (Str.length ());//take the subscript of STR            CharC=str.charat (index);//take the character according to the subscriptSf.append (c);//character into a string buffer        }        returnSf.tostring ();//Convert to String return    }}

Web. XML configuration file

<servlet>    <Servlet-name>Checkcodeservlet</Servlet-name>    <Servlet-class>Web. Checkcodeservlet</Servlet-class>  </servlet>  <servlet-mapping>    <Servlet-name>Checkcodeservlet</Servlet-name>    <Url-pattern>/checkcode</Url-pattern>  </servlet-mapping>

text.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

  

Java Code Verification Code

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.