Java Web development: randomly generated verification codes, supporting uppercase and lowercase letters and numbers; random Fonts

Source: Internet
Author: User

Java generates a Random verification code, which can generate numbers, uppercase letters, and lowercase letters at random. You can also randomly generate text fonts and sizes. The font size may vary depending on the image. </P> <p> package COM. hoo. util; </P> <p> Import Java. AWT. color; <br/> Import Java. AWT. font; <br/> Import Java. AWT. graphics; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. util. random; </P> <p> Import javax. imageIO. imageIO; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/>/** <br/> * <B> function: </B> Verification Code Generation Tool <br/> * @ project NetworkService <br/> * @ package COM. hoo. util <br/> * @ filename validcodeutils. java <br/> * @ createdate 2010-8-3 03:05:50 <br/> * @ author hoojo <br/> */<br/> @ suppresswarnings ("UNUSED ") <br/> public class validcodeutils {<br/> /*************************** **************************************** ** <br/> * Verification Code width <br/> */<br/> Public static int width = 60; <br/>/*** <br/> * Height of the Verification Code <br/> */<br/> Public static int Height = 20; </P> <p> /******************************** ************************************ <br /> * The Verification Code background color color_fc_bg should be smaller than color_bc_bg <br/> */<br/> Public static int color_fc_bg = 200; <br/>/**** <br/> * the background color of the Verification Code color_fc_bg should be smaller than color_bc_bg <br/> */<br/> Public static int color_bc_bg = 250; </P> <p> /******************************** ************************************ <br /> * the color of the Verification Code background interference line color_fc_line should be less than color_bc_line <br/> */<br/> Public static int color_fc_line = 160; <br/>/**** <br/> * the color of the background interference line of the Verification Code color_fc_line should be less than color_bc_line <br/> */<br/> Public static int color_bc_line = 200; </P> <p> /******************************** **************************************** * ** <br/> * The verification code color color_fc_code should be smaller than color_bc_code <br/> */<br/> Public static int color_fc_code = 20; <br/>/**** <br/> * the color of the Verification Code color_fc_code should be less than color_bc_code <br/> */<br/> Public static int color_bc_code = 170; </P> <p> /******************************** **************************************** * ** <br/> * generate the color in the specified range <br/> * @ Param FC range FC color value less than 255 <br/> * @ Param BC range BC color value less than 255 <br/> * @ return color <br/> */<br/> Private Static color getrandcolor (int fc, int BC) {<br/> random = new random (); <br/> If (FC <0) <br/> fc = 0; <br/> If (Bc <0) <br/> BC = 1; <br/> If (FC> 255) <br/> fc = 255; <br/> If (BC> 255) <br/> BC = 255; <br/> If (BC = FC) <br/> BC + = 10; <br/> int temp = 0; <br/> If (Bc <FC) {<br/> temp = BC; <br/> BC = FC; <br/> fc = temp; <br/>}< br/> int r = FC + random. nextint (BC-Fc); <br/> int G = FC + random. nextint (BC-Fc); <br/> int B = FC + random. nextint (BC-Fc); <br/> return new color (R, G, B ); <br/>}</P> <p>/** <br/> * <B> function: </B> Image Generation Method <br/> * @ createdate 2010-8-3 03:06:22 <br/> * @ author hoojo <br/> * @ Param request httpservletrequest <br/> *@ param response httpservletresponse <br/> * @ return Boolean <br/> * @ throws exception <br/> */<br/> Public static Boolean getimage (httpservletrequest request, httpservletresponse response) throws exception {<br/> response. reset (); <br/> response. setcontenttype ("image/JPEG"); <br/> // set the page to not be cached <br/> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setdateheader ("expires", 0); <br/> // create an image in the memory <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); </P> <p> // obtain the image context <br/> graphics IMG = image. getgraphics (); <br/> // generate a random class <br/> random = new random (); </P> <p> // set the background color <br/> IMG. setcolor (getrandcolor (color_fc_bg, color_bc_bg); <br/> IMG. fillrect (0, 0, width, height); </P> <p> // set the font <br/> IMG. setfont (new font ("Times New Roman", Font. plain, 18); </P> <p> // draw a border <br/> // G. setcolor (new color (); <br/> // G. drawrect (155, width-1, height-1); </P> <p> // generates random interference lines, make the authentication code in the image difficult to be detected by other programs <br/> IMG. setcolor (getrandcolor (color_fc_line, color_bc_line); <br/> for (INT I = 0; I <155; I ++) {<br/> int x = random. nextint (width); <br/> int y = random. nextint (height); <br/> int XL = random. nextint (12); <br/> int yl = random. nextint (12); <br/> IMG. drawline (X, Y, x + XL, Y + yl); <br/>}</P> <p> // obtain the random ID code (4 digits) <br/> string codevalue = ""; <br/> for (INT I = 0; I <4; I ++) {<br/> // string Rand = string. valueof (random. nextint (10); <br/> string Rand = getrandomchar (); <br/> codevalue = codevalue. concat (RAND); <br/> IMG. setfont (getrandomfont (); // random font <br/> // display the verification code in the image <br/> IMG. setcolor (getrandcolor (color_fc_code, color_bc_code); <br/> IMG. drawstring (RAND, 13 * I + 6, 16); <br/>}< br/> request. getsession (). setattribute ("codevalue", codevalue); <br/> // image effective <br/> IMG. dispose (); <br/> // output the image to the page <br/> return ImageIO. write (image, "Jpeg", response. getoutputstream (); <br/>}</P> <p>/** <br/> * randomly generated characters, uppercase, lowercase, and numbers <br/> * <B> function: </B> function <br/> * @ createdate 2010-8-23 10:33:55 <br/> * @ author hoojo <br/> * @ return <br/> */<br/> public static string getrandomchar () {<br/> int Index = (INT) math. round (math. random () * 2); <br/> string randchar = ""; <br/> switch (INDEX) {<br/> case 0: // uppercase characters <br/> randchar = string. valueof (char) math. round (math. random () * 25 + 65); <br/> break; <br/> case 1: // lower case character <br/> randchar = string. valueof (char) math. round (math. random () * 25 + 97); <br/> break; <br/> default: // number <br/> randchar = string. valueof (math. round (math. random () * 9); <br/> break; <br/>}< br/> return randchar; <br/>}</P> <p>/** <br/> * <B> function: </B> randomly generated font and text size <br/> * @ createdate 10:44:22 <br/> * @ author hoojo <br/> * @ return <br/> * /<br/> Public static font getrandomfont () {<br/> string [] fonts = {"Georgia", "verdana", "Arial", "tahoma", "Time News Roman", "Courier New ", "Arial black", "quantzite" };< br/> int fontindex = (INT) math. round (math. random () * (fonts. length-1); <br/> int fontsize = (INT) math. round (math. random () * 4 + 16); <br/> return new font (fonts [fontindex], Font. plain, fontsize); <br/>}< br/>

The verification code value is saved in the session: request. getsession (). setattribute ("codevalue", codevalue );
Check whether the value entered by the user is equal to the codevalue in the session;

The following is a JSP page call servlet: validcodeservlet. Java

Validcodeservlet calls the above validcodeutils Verification Code Generation Tool class

Package COM. hoo. servlet; </P> <p> Import Java. io. ioexception; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import COM. hoo. util. validcodeutils; </P> <p> @ suppresswarnings ("serial") <br/> public class validcodeservlet extends httpservlet {</P> <p> Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> try {<br/> validcodeutils. getimage (request, response); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> doget (request, response); <br/>}< br/>

 

Call the servlet method on the JSP page.

 JS: reloadvalidcode method </P> <p> function reloadvalidcode (o) {<br/> O. src = "$ {pagecontext. Request. contextpath}/validcodeservlet? Timed = "+ new date (). getmilliseconds (); <br/>}< br/> here, "timed =" + new date (). getmilliseconds (); is the HTML tag used to prevent ie caching </P> <p>: <br/> <br/> directly configure the URL with the servlet name, and Web. xml configuration. The main call path $ {pagecontext. Request. contextpath}/validcodeservlet will carry the root directory, which is relatively safe. </P> <p> Web. validcodeservlet configuration in XML <br/> <servlet> <br/> <servlet-Name> validcodeservlet </servlet-Name> <br/> <servlet-class> COM. hoo. servlet. validcodeservlet </servlet-class> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> validcodeservlet </Servlet -Name> <br/> <URL-pattern>/validcodeservlet </url-pattern> <br/> </servlet-mapping>

 

Now, the entire image verification code is configured. Enter the URL you requested in the browser and you will see the effect on the JSP page.

 

What is the result. Font, size, number, uppercase/lowercase characters, and color!

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.