Java EE generates the verification code for the website form, javaee

Source: Internet
Author: User

Java EE generates the verification code for the website form, javaee

Today, most websites or Web applications have implemented the verification code function. Adding the verification code can prevent hackers from using malicious programs to perform frequent logon, registration, and irrigation operations on the website.

The following example mainly uses Jsp and Servlet technologies in Java EE, hoping to help beginners.

The procedure is as follows:

1. Create a web project under MyEclipse. The project directory structure is as follows:

 

2. Create a package namedCom. servletAnd then createServlet type, Named:ValidateCodeServlet,This class is used to generate Verification Code images.

The code and comments are as follows: ValidateCodeServlet. java

1 package com. servlet; 2 3 import java. awt. color; 4 import java. awt. font; 5 import java. awt. graphics; 6 import java. awt. image. bufferedImage; 7 import java. io. IOException; 8 import java. io. printWriter; 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 17 public class ValidateCodeServlet extends HttpServlet {18 19 20 public void doGet (HttpServletRequest request, HttpServletResponse response) 21 throws ServletException, IOException {22 doPost (request, response ); 23 24} 25 26 27 public void doPost (HttpServletRequest request, HttpServletResponse response) 28 throws ServletException, IOException {29 // disable page cache 30 response. setHeader ("Pragma", "No-cache"); 31 response. setHeader ("Cache-Control", "No-cache"); 32 response. setDateHeader ("Expires", 0); 33 response. setContentType ("image/jpeg"); // set the MIME type of the response body to image 34 35 int width = 60, height = 20; 36 // create an image in the cache, the width is 60, and the height is 2037 BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); 38 // obtain the object used to process the graphic context, equivalent to the brush 39 Graphics g = image. getGraphics (); 40 Random random = new Random (); // Random number object 41g. setColor (getRandomColor (200,250); // set the background color of the image to 42 GB. fillRect (0, 0, width, height); // draw a rectangle, coordinate (0, 0), width is 60, height is 2043g. setFont (new Font ("Times New Roman", Font. PLAIN, 18); // set the font format to 44 GB. setColor (getRandomColor (160,200); 45 for (int I = 0; I <130; I ++) {// generates 130 interference lines 46 int x = random. nextInt (width); 47 int y = random. nextInt (height); 48 int x1 = random. nextInt (12); // nextInt (12) represents a random number of int types ranging from 0 to 12. 49 int y1 = random. nextInt (12); 50g. drawLine (x, y, x + x1, y + y1); // coordinates (x, y) and coordinates (x + x1, y + y1) of the image) draw interference lines between 51} 52 String strCode = ""; 53 for (int I = 0; I <4; I ++) {54 String strNumber = String. valueOf (random. nextInt (10); 55 strCode = strCode + strNumber; 56 // set the font color to 57 GB. setColor (new Color (15 + random. nextInt (120), 15 + random. nextInt (120), 15 + random. nextInt (120); 58g. drawString (strNumber, 13 * I + 6, 16); // draw the verification code on the image in sequence, with the coordinates (13 * I + 6, 16) 59} 60 request. getSession (). setAttribute ("Code", strCode); // keep the verification Code to 61 GB in ssesion. dispose (); // release the context of the image and the system resources it uses 62 ImageIO. write (image, "JPEG", response. getOutputStream (); // output the JPEG image 63 response. getOutputStream (). flush (); // refresh the output stream 64 response. getOutputStream (). close (); // close the output stream 65 66} 67 68 // obtain the random Color of 69 public Color getRandomColor (int fc, int bc) {70 Random random = new Random (); 71 Color randomcolor = null; 72 if (fc> 255) 73 fc = 255; 74 if (bc> 255) 75 bc = 255; 76 // set the random color value between 0 and 25 5. 77 int r = fc + random. nextInt (bc-fc); 78 int g = fc + random. nextInt (bc-fc); 79 int B = fc + random. nextInt (bc-fc); 80 randomcolor = new Color (r, g, B); 81 return randomcolor; // return the opaque sRGB color with red, blue, and green. 82} 83 84 85 86}ValidateCodeServlet. java

 

3. Create a new form registration page index. jsp. in the form on this page, use the src attribute of the label to call the image verification code generated by the ValidateCodeServlet class.

The Code is as follows:

1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8  

The running effect is as follows:

 

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.