Verification codes are very commonly used in websites. They are mainly used to effectively prevent a specific registered user from using brute-force password cracking methods for continuous login attempts.
The Demo code consists of the following three parts:
1. checkCode. java: used to generate a verification code
2. checkCodeServler
3. check. jsp Verification
The content of checkCode. java is as follows:
1 // obtain a four-digit random number 2 private char mapTable [] = {'0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9'}; 3 4 // generate the verification code and return the randomly generated number 5 public String getEnsure (int width, int height, OutputStream OS) {6 if (width <= 0) 7 width = 60; 8 if (height <= 0) 9 height = 20; 10 11 BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); 12 13 // obtain the image context 14 Graphics g = image. getGraphics (); 15 16 // set the background color to 17 GB. setColor (new Color (0 xDCCCCC); 18g. fillRect (0, 0, width, height); 19 20 // draw the border 21g. setColor (Color. black); 22g. drawRect (0, 0, width-1, height-1); 23 24 // obtain the random ID code 25 String strEnsure = ""; 26 27 // 4 represents 4-digit Verification Code 28 for (int I = 0; I <4; ++ I) {29 strEnsure + = mapTable [(int) (mapTable. length * Math. random ()]; 30} 31 32 // display the authentication code to 33g in the image. setColor (Color. red); 34g. setFont (new Font ("Atlantic Inline", Font. PLAIN, 14); 35 36 // The exact coordinate of the painting 37 String str = strEnsure. substring (0, 1); 38g. drawString (str, 8, 14); 39 str = strEnsure. substring (1, 2); 40g. drawString (str, 20, 15); 41 str = strEnsure. substring (2, 3); 42g. drawString (str, 35, 18); 43 str = strEnsure. substring (3, 4); 44g. drawString (str, 45, 15); 45 46 // release the graphic context 47g. dispose (); 48 49 try {50 // output image to page 51 ImageIO. write (image, "JPEG", OS); 52} catch (IOException e) {53 return ""; 54} 55 56 return strEnsure; // return the generated random number 57}
The content of checkCodeServlet
1 public void doGet (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 doPost (request, response); 4} 5 6 public void doPost (HttpServletRequest request, response) 7 throws ServletException, IOException {8 // disable the cache. Every time you access this page, a new 9 response is generated. setHeader ("Pragma", "No-cache"); 10 response. setHeader ("Cache-Control", "no-cache"); 11 response. setDateHeader ("Expires", 0); 12 13 // The instance object that generates the Verification Code 14 CheckCode ie = new CheckCode (); 15 16 // call the method in, the String 17 String str = ie in the generated verification code is returned. getEnsure (0, 0, response. getOutputStream (); 18 19 // get the session and save the string in the session to make a basic comparison. 20 HttpSession session = request. getSession (); 21 session. setAttribute ("strEnsure", str); 22 23}
Then configure servlet in web. xml
1 <servlet>2 <servlet-name>CheckServlet</servlet-name>3 <servlet-class>com.blog.servlet.CheckServlet</servlet-class>4 </servlet>5 <servlet-mapping> 6 <servlet-name>CheckServlet</servlet-name> 7 <url-pattern>/check</url-pattern> 8 </servlet-mapping>
Finally, the reference of the jsp page
1
On the jsp page, you only need to point the src attribute of img to the servlet that generates the verification code, pointing to the servle on the web. xmlt ing url (here I have struggled for a long time ...)