Generate verification code with Servlet

Source: Internet
Author: User
Package COM. work. core. image; import Java. AWT. color; import Java. AWT. font; import Java. AWT. graphics; import Java. AWT. image. bufferedimage; import Java. io. ioexception; import Java. util. random; import javax. imageIO. imageIO; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. servlet. h TTP. httpsession; import org. Apache. commons. Logging. log; import org. Apache. commons. Logging. logfactory;/*** @ author wangmj * generates a Random verification code! Prevents profiteering cracking. */Public class authimage extends httpservlet {Private Static log = logfactory. getlog (authimage. class);/*****/Private Static final long serialversionuid = 8165458985542870320l; // set the font size of the string in the graphic Verification Code private font mfont = new font ("Arial black ", font. plain, 16); Public void Init () throws servletexception {super. init ();}/*** generate random color * @ Param FC * @ Param BC * @ return */color getrandcolor (int fc, int B C) {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);}/* (non-javadoc) * @ see javax. servlet. HTTP. httpservlet # Service (javax. servlet. HTTP. httpservletrequest, javax. servlet. HTTP. httpservletresponse) */Public void Service (httpservletrequest re Quest, httpservletresponse response) throws servletexception, ioexception {// generate the server's corresponding service method // block the generated page content from being cached, ensure that the random verification code response is regenerated each time. setheader ("Pragma", "No-Cache"); response. setheader ("cache-control", "No-Cache"); response. setdateheader ("expires", 0); response. setcontenttype ("image/JPEG"); // specify the image size of the graphic Verification Code; int width = 80; // width: int Height = 20; // height bufferedimage image = new bufferedimage (width, height, buffere Dimage. type_int_rgb); // prepare to draw graphics G = image in the image. getgraphics (); random = new random (); G. setcolor (getrandcolor (200,250); G. fillrect (1, 1, width-1, height-1); G. setcolor (new color (102,102,102); G. drawrect (0, 0, width-1, height-1); G. setfont (mfont); G. setcolor (getrandcolor (160,200); // generate a random line for (INT I = 0; I <155; I ++) {int x = random. nextint (width-1); int y = random. nextint (heigh T-1); int XL = random. nextint (6) + 1; int yl = random. nextint (12) + 1; G. drawline (X, Y, x + XL, Y + yl) ;}for (INT I = 0; I <70; I ++) {int x = random. nextint (width-1); int y = random. nextint (height-1); int XL = random. nextint (12) + 1; int yl = random. nextint (6) + 1; G. drawline (X, Y, X-XL, Y-yl);} string srand = ""; // generate a random string and add it to the image int Len = 2; // control the length of the random code for (INT I = 0; I <Len; I ++) {string TMP = Getrandomchar (); srand + = TMP; G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); G. drawstring (TMP, 15 * I + 10, 15);} httpsession session = request. getsession (true); log. debug ("randomly generated string" + srand); // convert it to lowercase automatically. That is to say, the user does not need to be case sensitive when entering the verification code, so it is convenient to enter the verification code. Session. setattribute ("randomimagestr", srand. tolowercase (); log. debug ("Get from session" + session. getattribute ("randomimagestr"); G. dispose (); ImageIO. write (image, "Jpeg", response. getoutputstream ();}/*** randomly generated string * @ return */private string getrandomchar () {int Rand = (INT) math. round (math. random () * 2); long itmp = 0; char CTMP = '/u0000'; Switch (RAND) {Case 1: itmp = math. round (math. random () * 25 + 65); CTMP = (char) itmp; return string. valueof (CTMP); Case 2: itmp = math. round (math. random () * 25 + 97); CTMP = (char) itmp; return string. valueof (CTMP); default: itmp = math. round (math. random () * 9); Return string. valueof (itmp );}}}
Configure this servlet in Web. xml
 <servlet>  <servlet-name>authImage</servlet-name>  <servlet-class>com.work.core.image.AuthImage</servlet-class> </servlet>
 <servlet-mapping>  <servlet-name>authImage</servlet-name>  <url-pattern>/authImage</url-pattern> </servlet-mapping>
 

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.