Java Generate Verification Code

Source: Internet
Author: User
Tags set background


Recently, a friend asked me how to use Java to implement the verification code, because he was eager to use, I have used before a JSP page in the generation of a code to generate the source codes to him, for everyone's convenience, now put the source of the following, interested friends can refer to. Friends can make some changes on this basis to achieve their desired effect.


identifycode.jsp


<%@ page contenttype= "image/jpeg" import= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding = "Utf-8"%><%!         Color getrandcolor (int fc,int BC) {//The given range gets a random color randomly random = new random ();         if (fc>255) fc=255;         if (bc>255) bc=255;         int R=fc+random.nextint (BC-FC);         int G=fc+random.nextint (BC-FC);         int B=fc+random.nextint (BC-FC);         return new Color (R,G,B); }%><%//Settings page does not cache Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0);//Create an image in memory, set the width and height of the verification code picture int width=90, height=28; BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);//Get the graphics context Graphics g = Image.getgraphics ();//Generate random class randomly random = new random ();//Set Background color G.setcolor (Getrandcolor (200,250));//fill a rectangular area, X, Y is the starting coordinate (that is, the upper-left coordinate), the following two parameters are: W, h, is the width and height of the rectangular region g.fillrect (0, 0, width, height);//Set the font, where the parameters: font, Style (Font.plain is one of the constants), the font size g.setfont (new Font ("Times New Roman", FONT.PLAIN,20);//randomly generate 155 lines of interference, so that the authentication code in the image is not easy to be detected by other programs G.setcolor (Getrandcolor (160,200)); for (int i=0;i<155;i++) {int x = R         Andom.nextint (width);         int y = random.nextint (height);         int xl = Random.nextint (12);         int yl = Random.nextint (12); G.drawline (x,y,x+xl,y+yl);} Take a randomly generated authentication code (6-Letter or number combination code) String codeList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";      String srand= ""; for (int i=0;i<6;i++) {int a=random.nextint (codelist.length ()-1);      String rand=codelist.substring (a,a+1);     Srand+=rand; Display the authentication code to the image in G.setcolor (new Color (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 only g.drawstring (rand,13*i+6,16) can be generated directly;} The authentication code will be stored in the session, in the background verification can be Session.setattribute ("Rand", SRand);//image takes effect G.dispose ();//Output image to page imageio.write (image, " JPEG ", Response.getoutputstream ()); Out.clear (); out = Pagecontext.pushbody ();%>



Put the above code in the JSP container can be run directly, generate random verification code picture. In the actual application, in the background program to remove the authentication code object from the session, and then the background to obtain the user input verification code string comparison, you can determine whether the user entered the correct verification code, and then the next step to perform the operation.



Java Generate Verification Code

Related Article

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.