Generate a verification code image using Servlet), servlet Verification Code

Source: Internet
Author: User

Use Servlet to generate Verification Code image (convert) and servlet Verification Code
Original article address: http://www.cnblogs.com/xdp-gacl/p/4108190.html1,BufferedImageClass Introduction

A verification code image is mainly usedBufferedImageClass:

Create a DrawImage Servlet to generate a verification code image.

1 package gacl. response. study; 2 import java. awt. color; 3 import java. awt. font; 4 import java. awt. graphics; 5 import java. awt. graphics2D; 6 import java. awt. image. bufferedImage; 7 import java. io. IOException; 8 import java. util. random; 9 import javax. imageio. imageIO; 10 import javax. servlet. servletException; 11 import javax. servlet. http. httpServlet; 12 import javax. servlet. http. httpServletRequest; 13 Import javax. servlet. http. httpServletResponse; 14/** 15 * generates a random image, which is used as the verification code 16 */17 public class DrawImage extends HttpServlet {18 private static final long serialVersionUID = 3034723796184546092l; 19 20 public static final int WIDTH = 120; // The WIDTH of the generated image 21 public static final int HEIGHT = 30; // The height of the generated image is 22 23 public void doGet (HttpServletRequest request, HttpServletResponse response) 24 throws Servle TException, IOException {25 this. doPost (request, response); 26} 27 28 public void doPost (HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException {30 String createTypeFlag = request. getParameter ("createTypeFlag"); // receives the createTypeFlag ID passed by the client 31 // 1. create an image in the memory 32 BufferedImage bi = new BufferedImage (WIDTH, HEIGHT, BufferedImage. TYPE_INT_RGB); 33 // 2. image 34 Graphics g = bi. getGraphics (); 35 // 3. set the background color of the image 36 setBackGround (g); 37 // 4. set the border of the image 38 setBorder (g); 39 // 5. draw the interference line on the image 40 drawRandomLine (g); 41 // 6. random Number 42 on the image // String random = drawRandomNum (Graphics2D) g, "ch"); // generate a Chinese Verification Code image 43 // String random = drawRandomNum (Graphics2D) g, "nl"); // generate a verification code image consisting of digits and letters 44 // String random = drawRandomNum (Graphics2D) g, "n "); // generate a digital verification code image 45 // String random = drawRandomNum ((Graphics2D) g, "l"); // generate a pure letter Verification Code image 46 String random = drawRandomNum (Graphics2D) g, createTypeFlag ); // generate the verification code image 47 Based on the createTypeFlag ID passed by the client. // 7. save the random number to 48 requests in the session. getSession (). setAttribute ("checkcode", random); 49 // 8. set the response header to notify the browser to open 50 response in the form of images. setContentType ("image/jpeg"); // equivalent to response. setHeader ("Content-Type", "image/jpeg"); 51 // 9. set the response header to control the browser against caching 52 response. setDateHeader ("expries",-1); 5 3 response. setHeader ("Cache-Control", "no-cache"); 54 response. setHeader ("Pragma", "no-cache"); 55 // 10. write the image to 56 ImageIO in the browser. write (bi, "jpg", response. getOutputStream (); 57} 58 59/** 60 * set the background color of the image 61 * @ param g 62 */63 private void setBackGround (Graphics g) {64 // set the color to 65 GB. setColor (Color. WHITE); 66 // fill area 67g. fillRect (0, 0, WIDTH, HEIGHT); 68} 69 70/** 71 * set the image's border 72 * @ param g 73 */74 Private void setBorder (Graphics g) {75 // set the border color to 76g. setColor (Color. BLUE); 77 // the border area is 78 GB. drawRect (1, 1, WIDTH-2, HEIGHT-2 ); 79} 80 81/** 82 * draw a random line on the image 83 * @ param g 84 */85 private void drawRandomLine (Graphics g) {86 // set the color to 87g. setColor (Color. GREEN); 88 // set the number of lines and draw the line 89 for (int I = 0; I <5; I ++) {90 int x1 = new Random (). nextInt (WIDTH); 91 int y1 = new Random (). nextInt (HEIGHT); 92 Int x2 = new Random (). nextInt (WIDTH); 93 int y2 = new Random (). nextInt (HEIGHT); 94g. drawLine (x1, y1, x2, y2 ); 95} 96} 97 98/** 99 * draw random character 100 * @ param g101 * @ param createTypeFlag102 * @ return103 * String... createTypeFlag is a variable parameter. In 104 * Java1.5, a new feature is added: Variable Parameter: Applicable to the situations where the number of parameters is uncertain and the type is determined. java treats variable parameters as arrays. Note: The variable parameter must be in the last 105 */106 private String drawRandomNum (Graphics2D g, String... createTypeFlag) {107 // set the color to 108 GB. setColor (Color. RED); 109 // set the font to 110 GB. setFont (new Font ("", Font. BOLD, 20 )); 111 // commonly used Chinese characters 112 String Highlight = "\ u7684 \ u4e00 \ u4e86 \ u662f \ u6211 \ yellow \ u5728 \ u4eba \ u4eec \ u6709 \ u6765 \ u4ed6 \ yellow \ u7740 \ u4e2a \ u5730 \ u5230 \ u5927 \ u91cc \ u8bf4 \ u5c31 \ u53bb \ u5b50 \ u5f97 \ u4e5f \ u548c \ u90a3 \ u8 981 \ Alibaba \ u65f6 \ u8fc7 \ u51fa \ u5c0f \ u4e48 \ u8d77 \ Alibaba \ u90fd \ u628a \ u597d \ Alibaba \ u591a \ u6ca1 \ Alibaba \ u53c8 \ u53ef \ alibaba \ u5b66 \ u53ea \ u4ee5 \ u4e3b \ u4f1a \ u6837 \ Alibaba \ u60f3 \ Alibaba \ u8001 \ Alibaba \ u4ece \ u81ea \ u9762 \ Alibaba \ u5934 \ u9053 \ alibaba \ u540e \ u7136 \ Users \ u50cf \ u89c1 \ Users \ u7528 \ u5979 \ u56fd \ u52a8 \ u8fdb \ u6210 \ u56de \ Users \ u5f00 \ u800c \ u5df1 \ u4e9b \ u73b0 \ u5c71 \ u6c11 \ u5019 \ Alibaba \ u53d1 \ u5de5 \ u5411 \ u4e8b \ u547d \ u7ed9 \ u957f \ Alibaba \ u58f0 \ Alibaba \ u7406 \ u773c \ u5fd7 \ alibaba \ u5fc3 \ u6218 \ Users \ u95ee \ u4f46 \ u8eab \ Alibaba \ Users \ u505a \ u53eb \ Users \ u542c \ u9769 \ Users \ u5462 \ u771f \ u5168 \ alibaba \ u56db \ Alibaba \ u6240 \ Alibaba \ u6700 \ u5149 \ u4ea7 \ u60c5 \ u8def \ Alibaba \ u603b \ u6761 \ Alibaba \ u8bdd \ u4e1c \ u5e2d \ Alibaba \ Users \ u5982 \ u88ab \ u82b1 \ u53e3 \ u653e \ u513f \ u5e3 8 \ u6c14 \ u4e94 \ u7b2c \ Alibaba \ u519b \ u5427 \ u6587 \ Alibaba \ u600e \ u5b9a \ Alibaba \ u884c \ u56e0 \ u522b \ u98de \ u5916 \ u6811 \ u00009 \ Alibaba \ u8239 \ u671b \ u65b0 \ u5e26 \ u961f \ u5148 \ Alibaba \ u7ad9 \ u4ee3 \ u5458 \ u673a \ u66f4 \ Alibaba \ u60a8 \ u6bcf \ u98ce \ u7ea7 \ u8ddf \ Alibaba \ u76f4 \ Alibaba \ u591c \ u6bd4 \ u9636 \ Alibaba \ u8f66 \ u91cd \ u4fbf \ u6597 \ u9a6c \ u54ea \ u5316 \ u5 92a \ u6307 \ u53d8 \ u793e \ u4f3c \ u58eb \ u8005 \ Alibaba \ u77f3 \ Alibaba \ u51b3 \ u767e \ Alibaba \ u62ff \ Users \ u516d \ u672c \ u601d \ u89e3 \ Users \ u6751 \ Users \ u96be \ u65e9 \ Users \ u5417 \ u6839 \ Users \ u76f8 \ u7814 \ u4eca \ u5176 \ Users \ u5750 \ u63a5 \ u5e94 \ u5173 \ u4fe1 \ u89c9 \ Users \ u53cd \ Users \ u9886 \ u6216 \ u5e08 \ Users \ u5757 \ Users \ u8c01 \ u8349 \ u8d8a \ u5b57 \ u52a0 \ u811a \ u7d27 \ u7231 \ u7b49 \ Users \ u9635 \ u6015 \ u6708 \ u9752 \ u534a \ u706b \ u6cd5 \ Users \ u5531 \ Users \ u5973 \ Users \ u4ef6 \ u611f \ u51c6 \ alibaba \ u56e2 \ u5c4b \ u79bb \ u8272 \ Alibaba \ u79d1 \ u5012 \ u775b \ u5229 \ Alibaba \ u521a \ Alibaba \ u7531 \ u9001 \ Alibaba \ u5bfc \ u665a \ u8868 \ Users \ u6574 \ Users \ u54cd \ u96ea \ u6d41 \ u672a \ u573a \ u8be5 \ u5e76 \ u5e95 \ Users \ u5fd9 \ u63d0 \ Users \ u4family \ u8f7b \ u8bb2 \ u519c \ u53e4 \ u9ed1 \ u544 A \ u754c \ u62c9 \ u540d \ u5440 \ Users \ u9633 \ Users \ u53f2 \ u6539 \ u5386 \ Users \ u753b \ u9020 \ u5634 \ Users \ u6cbb \ u5317 \ alibaba \ u670d \ u96e8 \ u7a7f \ u5185 \ Alibaba \ Users \ Alibaba \ u83dc \ Alibaba \ u7761 \ Alibaba \ u91cf \ u54b1 \ u89c2 \ u82e6 \ u4f53 \ u4f17 \ u901a \ u51b2 \ Alibaba \ u7834 \ u53cb \ Alibaba \ u672f \ u996d \ u516c \ u65c1 \ Alibaba \ u6781 \ Alibaba \ u67aa \ Alibaba \ u5c81 \ u7ebf \ u91ce \ Alibaba \ u7a7a \ u6536 \ u7b97 \ u81f3 \ u653f \ u57ce \ u52b3 \ u8 43d \ u94b1 \ u7279 \ u56f4 \ Alibaba \ u80dc \ u6559 \ u70ed \ Alibaba \ u5305 \ Alibaba \ u6e10 \ Alibaba \ u6570 \ Alibaba \ u6027 \ u97f3 \ u7b54 \ alibaba \ u9645 \ u65e7 \ u795e \ u5ea7 \ u7ae0 \ u5e2e \ Alibaba \ u7cfb \ Alibaba \ u53d6 \ Alibaba \ u5cb8 \ u6562 \ u6389 \ audio \ u79cd \ u88c5 \ u9876 \ u6025 \ u6797 \ u505c \ u606f \ u53e5 \ audio \ u8863 \ audio \ u62a5 \ u53f6 \ audio \ u6162 \ u53d4 \ u80cc \ audio "; 113 // combination of numbers and letters 114 String baseNumLetter = "01234 56789 ABCDEFGHJKLMNOPQRSTUVWXYZ "; 115 // pure number 116 String baseNum =" 0123456789 "; 117 // pure letter 118 String baseLetter =" ABCDEFGHJKLMNOPQRSTUVWXYZ "; 119 // createTypeFlag [0] = null indicates that the 120 if (createTypeFlag. length> 0 & null! = CreateTypeFlag [0]) {121 if (createTypeFlag [0]. equals ("ch") {122 // extract Chinese characters 123 return createRandomChar (g, baseChineseChar); 124} else if (createTypeFlag [0]. equals ("nl") {125 // returns the createRandomChar (g, baseNumLetter); 126} else if (createTypeFlag [0]. equals ("n") {128 // returns the number 129 return createRandomChar (g, baseNum); 130} else if (createTypeFlag [0]. equals ("l") {131 // returns createRandomChar (g, baseLetter ); 133} 134} else {135 // by default, the combination of digits and letters is truncated. 136 return createRandomChar (g, baseNumLetter); 137} 138 139 return ""; 140} 141 142/** 143 * create random character 144 * @ param g145 * @ param baseChar146 * @ return random character 147 */148 private String createRandomChar (Graphics2D g, String baseChar) {149 StringBuffer sb = new StringBuffer (); 150 int x = 5; 151 String ch = ""; 152 // control word count 153 for (int I = 0; I <4; I ++) {154 // set the font rotation angle to 155 int degree = new Random (). nextInt () % 30; 156 ch = baseChar. charAt (new Random (). nextInt (baseChar. length () + ""; 157 sb. append (ch); 158 // positive angle 159g. rotate (degree * Math. PI/180, x, 20); 160g. drawString (ch, x, 20); 161 // reverse angle 162g. rotate (-degree * Math. PI/180, x, 20); 163 x + = 30; 164} 165 return sb. toString (); 166} 167}

The running result is as follows:

  

2. Use the verification code image in the Form
1 <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> 2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3 

Running result:

  

In addition to a combination of letters and numbers, the DrawImage Servlet can also generate Verification Code images for Chinese characters, numbers, and letters, you only need to pass the agreed generation identifier parameter to the DrawImage Servlet, as shown below:

1 <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> 2 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3 

The running result is as follows:

  

Iii. server-side verification code for form submission
1 package gacl. response. study; 2 import java. io. IOException; 3 import javax. servlet. servletException; 4 import javax. servlet. http. httpServlet; 5 import javax. servlet. http. httpServletRequest; 6 import javax. servlet. http. httpServletResponse; 7/** 8 * @ author gacl 9 * after the server receives the verification code, it processes 10 */11 public class CheckServlet extends HttpServlet {12 public void doGet (HttpServletRequest request, HttpServletRespo Neuron response) 13 throws ServletException, IOException {14 String clientCheckcode = request. getParameter ("validateCode"); // receives the verification code 15 String serverCheckcode = (String) request submitted by the client browser. getSession (). getAttribute ("checkcode"); // retrieves the verification code 16 if (clientCheckcode. equals (serverCheckcode) {// compare the client verification code with the server verification code. If they are the same, the verification passes 17 System. out. println ("Verification Code passed! "); 18} else {19 System. out. println (" Verification Code Verification Failed! "); 20} 21} 22 23 public void doPost (HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException {25 doGet (request, response); 26} 27 28}

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.