User Logon Random verification code and user logon Verification Code

Source: Internet
Author: User

User Logon Random verification code and user logon Verification Code

1 public class CheckImage extends HttpServlet {2 // The width of the Verification Code image. 3 private int width = 94; 4 // height of the Verification Code image. 5 private int height = 44; 6 protected void service (HttpServletRequest req, HttpServletResponse resp) 7 throws ServletException, java. io. IOException {8 BufferedImage buffImg = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); 9 Graphics2D g = buffImg. createGraphics (); 10 // create a random number generator class. 11 Random random = new Random (); 12g. setColor (Color. WHITE); 13g. fillRect (, width, height); 14 // create a font. The font size depends on the Image height. 15 Font font = new Font ("Times New Roman", Font. PLAIN, 36); 16 // set the Font. 17g. setFont (font); 18 // draw the border. 19g. setColor (Color. BLACK); 20g. drawRect (160, width-1, height-1); 21 22 // generates random interference lines, making the identification code in the image hard to be detected by other programs. 23g. setColor (Color. GRAY); 24 for (int I = 0; I <160; I ++) {25 int x = random. nextInt (width); 26 int y = random. nextInt (height); 27 int xl = random. nextInt (12); 28 int yl = random. nextInt (12); 29g. drawLine (x, y, x + xl, y + yl); 30} 31 32 // randomCode is used to save the randomly generated verification code so that the user can perform verification after login. 33 StringBuffer randomCode = new StringBuffer (); 34 int red = 0, green = 0, blue = 0; 35 // a four-digit verification code is randomly generated. 36 for (int I = 0; I <4; I ++) {37 // obtain the randomly generated verification code number. 38 String strRand = String. valueOf (random. nextInt (10); 39 // generate a random color component to construct the color value, so that the color value of each output number will be different. 40 red = random. nextInt (210); 41 green = random. nextInt (150); 42 blue = random. nextInt (150); 43 // use a random color to plot the verification code into the image. 44g. setColor (new Color (red, green, blue); 45g. drawString (strRand, 20 * I + 8, 36); 46 // combine the four random numbers. 47 randomCode. append (strRand); 48} 49 // Save the four-digit verification code to the Session. 50 HttpSession session = req. getSession (); 51 session. setAttribute ("validate", randomCode. toString (); 52 // disable image caching. 53 resp. setHeader ("Pragma", "no-cache"); 54 resp. setHeader ("Cache-Control", "no-cache"); 55 resp. setDateHeader ("Expires", 0); 56 resp. setContentType ("image/jpeg"); 57 // output the image to the Servlet output stream. 58 ServletOutputStream sos = resp. getOutputStream (); 59 ImageIO. write (buffImg, "jpeg", sos); 60 sos. close (); 61} 62}

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.