JSP Chinese Verification Code

Source: Internet
Author: User
<% @ Page pageencoding = "gb2312" contenttype = "image/JPEG" Import = "javax. imageIO. *, Java. util. *, Java. AWT. image. *, Java. AWT. * "%> <%! // Obtain and generate a random color here. Color getrandcolor (random, int ff, int CC) {If (FF> 255) FF = 255; If (CC> 255) cc = 255; int r = FF + random. nextint (CC-ff); int G = FF + random. nextint (CC-ff); int B = FF + random. nextint (CC-ff); return new color (R, G, B) ;}%><%// set no cache response on the JSP page. setheader ("Pragma", "No-Cache"); response. setheader ("cache-control", "No-Cache"); response. setdateheader ("Expires", 0); // set the length and width of the image to int width = 130; int Height = 30; // you can specify the number of randomly selected Chinese characters. In this example, there are too many texts, I will not list them one by one, just for example. String base = "\ u9752 \ u534a \ u706b \ u6cd5 \ signature \ u5efa \ signature \ u5531 \ signature \ u5973 \ signature \ u4ef6 \ u611f \ u51c6 \ u97f3 \ u7b54 \ u54e5 \ u9645 \ u65e7 \ u795e \ u5ea7 \ u7ae0 \ u538b \ u6162 \ u53d4 \ u80cc \ u7ec6 "; // set the number of alternative random Chinese characters int length = base. length (); // create the cached image bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); // obtain the image object graphics G = image. getgraphics (); // create the random function random = new random (); // set the image background color G. setcolor (getrandcolor (randdom, 188,235); G. fillrect (0, 0, width, height); // set the random alternative font type string [] fonttypes = {"\ u5b8b \ u4f53 ", "\ u65b0 \ u5b8b \ u4f53", "\ u9ed1 \ u4f53", "\ u6977 \ u4f53", "\ u96b6 \ u4e66"}; int fontpeslength = fonttypes. length; // Add noise on the image background to increase the difficulty of image analysis. setcolor (getrandcolor (randdom, 180,199); G. setfont (new font ("Times New Roman", Font. plain, 14); For (INT I = 0; I <4; I ++) {G. drawstring ("@ *", 0, 5 * (I + 2 ));} // obtain the randomly generated Verification Code (4 Chinese characters) // Save the Generated Chinese character string srand = ""; for (INT I = 0; I <4; I ++) {int start = random. nextint (length); string Rand = base. substring (start, start + 1); srand + = rand; // set the font color G on the image. setcolor (getrandcolor (random, 10,150); // set the font format G. setfont (new font (fonttypes [random. nextint (fonttypeslength)], Font. bold, 18 + random. nextint (6); // draw the Chinese character to the verification image G. drawstring (RAND, 24 * I + 10 + random. nextint (8), 24);} // Save the verification code to the session in s ession. setattribute ("RAND", srand); G. dispose (); // output the image to the ImageIO on the JSP page. write (image, "Jpeg", response. getoutputstream (); // close the stream out. clear (); out = pagecontext. pushbody (); %>

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.