Java_web: code generated for the verification code; java_web verification code generated

Source: Internet
Author: User

Java_web: code generated for the verification code; java_web verification code generated

1 package com. itheima. servlet; 2 3 import java. awt. color; 4 import java. awt. font; 5 import java. awt. graphics; 6 import java. awt. graphics2D; 7 import java. awt. image. bufferedImage; 8 import java. io. IOException; 9 import java. util. random; 10 11 import javax. imageio. imageIO; 12 import javax. servlet. servletException; 13 import javax. servlet. http. httpServlet; 14 import javax. servlet. http. httpServletRequest; 15 import javax. servlet. http. httpServletResponse; 16 import javax. servlet. http. httpSession; 17 18/** 19 * generate Verification Code image 20*21*22 */23 public class CheckImgServlet extends HttpServlet {24 25 public void doGet (HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException {27 28 29 30 31 32 // disable cache 33 // response. setHeader ("Cache-Control", "no-cache"); 34 // response. setHeader ("Pragma", "no-cache"); 35 // response. setDateHeader ("Expires",-1); 36 37 int width = 120; 38 int height = 30; 39 40 // Step 1 draw a memory image 41 BufferedImage bufferedImage = new BufferedImage (width, height, 42 BufferedImage. TYPE_INT_RGB); 43 44 // Step 2. Draw the background color through the Drawing Object 45 Graphics graphics = bufferedImage. getGraphics (); // get the Drawing Object --- paint brush 46 // you must specify a color 47 graphics before drawing any image. setColor (getRandColor (200,250); 48 graphics. fillRect (0, 0, width, height); 49 50 // step 3 draw the border 51 graphics. setColor (Color. WHITE); 52 graphics. drawRect (0, 0, width-1, height-1); 53 54 // Step 4 four random numbers 55 Graphics2D graphics2d = (Graphics2D) graphics; 56 // set the output font 57 graphics2d. setFont (new Font ("", Font. BOLD, 18); 58 59 String words = 60 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "; 61 // generate the verification code for Chinese characters 62/* String words = "\ u7684 \ u4e00 \ u4e86 \ u662f \ u6211 \ signature \ u5728 \ u4eba \ u4eec \ u6709 \ u6765 \ u4ed6 \ \ Alibaba \ u7740 \ Alibaba \ u5730 \ u5230 \ u5927 \ u91cc \ u8bf4 \ u5c31 \ u53bb \ u5b50 \ u5f97 \ Alibaba \ u90a3 \ u8981 \ Alibaba \ Users \ u51fa \ u5c0f \ u4e48 \ u8d77 \ u4f60 \ u90fd \ u628a \ u597d \ Users \ u591a \ u6ca1 \ Users \ u53c8 \ u53ef \ Users \ u53ea \ Users \ Users \ u6837 \ u5e74 \ u60f3 \ u751f \ u540c \ u8001 \ u4e2d \ Users \ u4ece \ u81ea \ u9762 \ Users \ u5934 \ u9053 \ Users \ u50cf \ u89c1 \ Users \ u7528 \ u5979 \ u56fd \ u52a8 \ u8fdb \ u6210 \ u56de \ u4ec0 \ u8fb9 \ u4f5c \ Users \ u800c \ Users \ Users \ u5019 \ u7ecf \ u53d1 \ u5de5 \ u5411 \ u4e8b \ u547d \ u7ed9 \ u957f \ u6c34 \ Users \ u58f0 \ Users \ u773c \ u5fd7 \ u70b9 \ u5fc3 \ u6218 \ Users \ u95ee \ Users \ u505a \ u53eb \ Users \ u542c \ u9769 \ u6253 \ u5462 \ Users \ u5168 \ u624d \ u56db \ u5df2 \ u6240 \ u654c \ u4e4b \ u6700 \ u5149 \ Users \ u60c5 \ u8def \ Users \ u603b \ u6761 \ Users \ Users \ u4eb2 \ u5982 \ u88ab \ u82b1 \ u53e3 \ u653e \ u513f \ u5e38 \ u6c14 \ Users \ u7b2c \ Users \ u5427 \ u6587 \ Users \ u600e \ u5b9a \ u8bb8 \ u5feb \ Users \ u884c \ Users \ u98de \ u5916 \ u6811 \ u00009 \ Users \ u5f80 \ u8239 \ u671b \ Users \ Users \ u961f \ u5148 \ u529b \ Users \ u5374 \ u7ad9 \ u4ee3 \ u5458 \ u673a \ u66f4 \ Users \ u60a8 \ Users \ u98ce \ Users \ Users \ u5c11 \ u76f4 \ u610f \ u591c \ u6bd4 \ u9636 \ u8fde \ Users \ u91cd \ Users \ u6597 \ Users \ u54ea \ u5316 \ Users \ u793e \ Users \ u58eb \ u8005 \ u5e72 \ u77f3 \ u6ee1 \ Users \ u51b3 \ Users \ u62ff \ Users \ u672c \ u601d \ u89e3 \ u7acb \ u6cb3 \ u6751 \ Users \ u96be \ u65e9 \ u8bba \ u5417 \ u6839 \ u5171 \ u8ba9 \ u76f8 \ u7814 \ u4eca \ Users \ u5750 \ u63a5 \ Users \ u89c9 \ Users \ u53cd \ u5904 \ u8bb0 \ u5c06 \ Alibaba \ u9886 \ u6216 \ Alibaba \ u5757 \ Alibaba \ u8d8a \ u5b57 \ u52a0 \ u811a \ u7d27 \ u7231 \ Users \ u9635 \ u6015 \ u6708 \ u9752 \ Users \ u706b \ u6cd5 \ u9898 \ u5efa \ u8d76 \ Users \ u5531 \ Users \ u5973 \ Users \ Users \ u51c6 \ u5f20 \ u56e2 \ u5c4b \ u79bb \ u8272 \ Users \ u79d1 \ u5012 \ u775b \ u5229 \ Users \ u521a \ Users \ u7531 \ u9001 \ Users \ tags \ u665a \ u8868 \ u591f \ u6574 \ u8ba4 \ u54cd \ u96ea \ u6d41 \ u672a \ u573a \ u8be5 \ u5e76 \ tags \ u786e \ u8fd1 \ u4family \ Alibaba \ u53e4 \ Alibaba \ u544a \ Alibaba \ u62c9 \ Alibaba \ u5440 \ Alibaba \ u9633 \ u7167 \ u529e \ u53f2 \ u6539 \ Users \ u753b \ u9020 \ u5634 \ u6b64 \ u6cbb \ u5317 \ u5fc5 \ u670d \ u96e8 \ u7a7f \ Users \ u83dc \ Users \ u7761 \ Alibaba \ u91cf \ u54b1 \ u89c2 \ u82e6 \ u4f53 \ u4f17 \ u901a \ u51b2 \ Alibaba \ u7834 \ u53cb \ Alibaba \ u672f \ u996d \ Alibaba \ u65c1 \ Alibaba \ u6781 \ Alibaba \ u67aa \ u8bfb \ u6c99 \ u5c81 \ u7ebf \ u91ce \ Alibaba \ u6536 \ Alibaba \ u81f3 \ Alibaba \ u57ce \ u52b3 \ u843d \ u94b1 \ Alibaba \ u56f4 \ u80dc \ u6559 \ u70ed \ u5c55 \ u5305 \ Alibaba \ u6570 \ Alibaba \ u6027 \ u97f3 \ u7b54 \ u54e5 \ u9645 \ u65e7 \ u795e \ u7ae0 \ u5e2e \ u5566 \ u53d7 \ u7cfb \ Users \ u53d6 \ Users \ u6users \ Users \ u5ffd \ u79cd \ u88c5 \ u9876 \ u6025 \ u6797 \ u505c \ u606f \ u53e5 \ u533a \ u8863 \ u822c \ u62a5 \ u53f6 \ u538b \ u6162 \ u53d4 \ u80cc \ u7ec6 "; */63 Random random = new Random (); // generate Random number 64 // StringBuffer sb = new StringBuffer (); 65 66 // define x coordinate 67 int x = 10; 68 for (int I = 0; I <4; I ++) {69 // random color 70 graphics2d. setColor (new Color (20 + random. nextInt (110), 20 + random 71. nextInt (110), 20 + random. nextInt (110); 72 // rotate-30 --- 30 degrees 73 int jiaodu = random. nextInt (60)-30; 74 // converts radians 75 double theta = jiaodu * Math. PI/180; 76 77 // generate a random number 78 int index = random. nextInt (words. length (); // generate a random number 0 to length-1 79 // obtain the letter and number 80 char c = words. charAt (index); 81 82 // Add the Generated Chinese characters to buffer 83 // sb. append (c); 84 85 // Output c to image 86 graphics2d. rotate (theta, x, 20); 87 graphics2d. drawString (String. valueOf (c), x, 20); 88 graphics2d. rotate (-theta, x, 20); 89 x + = 30; 90} 91 92 // request. getSession (). setAttribute ("code", sb. toString (); 93 // Step 5 draw the interference line 94 graphics. setColor (getRandColor (160,200); 95 int x1; 96 int x2; 97 int y1; 98 int y2; 99 for (int I = 0; I <30; I ++) {100x1 = random. nextInt (width); 101x2 = random. nextInt (12); 102 y1 = random. nextInt (height); 103 y2 = random. nextInt (12); 104 graphics. drawLine (x1, y1, x1 + x2, x2 + y2); 105} 106 107 // output the image above to the browser ImageIO108 graphics. dispose (); // release the 109 ImageIO resource. write (bufferedImage, "jpg", response. getOutputStream (); 110 111} 112 113 114 public void doPost (HttpServletRequest request, HttpServletResponse response) 115 throws ServletException, IOException {116 doGet (request, response ); 117} 118 119/** 120 * fetch the color121 * 122 * @ param fc123 * int range parameter 1124 * @ param bc125 * int range parameter 2126 * @ return Color127 */128 private Color getRandColor (int fc, int bc) {129 // obtain its Random color 130 random Random = new Random (); 131 if (fc> 255) {132 fc = 255; 133} 134 if (bc> 255) {135 bc = 255; 136} 137 int r = fc + random. nextInt (bc-fc); 138 int g = fc + random. nextInt (bc-fc); 139 int B = fc + random. nextInt (bc-fc); 140 return new Color (r, g, B); 141} 142 143}

 

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.