JSP verification code generation click to update

Source: Internet
Author: User

Click "cannot see or change one"

Test cases:

Index. jsp

<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. util. * "errorpage =" "%> <HTML> Package assumervlet; 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; public class imagemaskservlet Extends httpservlet {public void doget (httpservletrequest request, response) throws servletexception, ioexception {dopost (request, response);} public void dopost (httpservletrequest request, response) throws servletexception, ioexception {response. setheader ("Pragma", "No-Cache"); response. setheader ("cache-control", "No-Cache"); response. setdateheader ("expires", 0); Int width = 60, Height = 20; // create an image bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); // obtain the graphic context graphics = image. getgraphics (); // generate random class random = new random (); // set the background color graphics. setcolor (getrandcolor (200,250); // the size of the Verification Code graphics. fillrect (0, 0, width, height); // set the font graphics. setfont (new font ("Times New Roman", Font. plain, 18); // graphics. setcolor (getrandcolo R (160,200); // generates 155 random interference lines, making it difficult for other programs to detect the for (INT I = 0; I <155; I ++) in the image) {int x = random. nextint (width); int y = random. nextint (height); int XL = random. nextint (12); int yl = random. nextint (12); graphics. drawline (X, Y, x + XL, Y + yl);} // obtain the random ID code (4 digits) string srand = ""; for (INT I = 0; I <4; I ++) {// 0 ~ 9 string Rand = string. valueof (random. nextint (10); srand + = rand; // sets the digital color graphics. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); // draw a random 4-Digit Image in graphics. drawstring (RAND, 13 * I + 6, 16);} // put the request in the session. getsession (). setattribute ("imagemask", srand); // The image takes effect for graphics. dispose (); // output the image to the ImageIO page. write (image, "Jpeg", response. getoutputstream ();} // generate random color public color getrandcolor (int fc, int BC) {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 );}}


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.