Java's Response to send a verification code case

 The definition of a Servlet code used to generate two-dimensional images in memory, and page output browser.

 1 import javax.imageio.ImageIO;
 2 import javax.servlet.ServletException;
 3 import javax.servlet.annotation.WebServlet;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.awt.*;
 8 import java.awt.image.BufferedImage;
 9 import java.io.IOException;
10 import java.util.Random;
11 
12 @WebServlet("/checkCodeServlet")
 13 is  public  class CheckCodeServlet the extends the HttpServlet {
 14      protected  void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
 15  
16          // definition picture width and height 
. 17          int width = 100 ;
 18 is          int height = 50 ;
 . 19  
20 is          // . 1 creating an object, generating images (CAPTCHA objects) in memory 
21 is          the BufferedImage image = new new the BufferedImage (width, height, BufferedImage.TYPE_INT_BGR);
 22 is  
23 is          // 2 modified image
24          // 2.1 fill the background color 
25          Graphics image.getGraphics G = (); // Get brush object 
26 is          g.setColor (Color.pink);   // set the color pen 
27          g.fillRect (0,0, width, height) ;   // draw a rectangle, with width and height coordinates given
 28  
29          // 2.2 Videos bezel 
30          g.setColor (Color.BLUE);     // set the color pen 
31 is          g.drawRect (0,0,. 1-width, height- 1);   // for image drawing frame 
32  
33 is          String STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789" ;
 34 is          // generate random subscript 
35         RAN = the Random new new the Random ();
 36  
37 [          for ( int I =. 1; I <=. 4; I ++ ) {
 38 is              int index = ran.nextInt (str.length ());
 39              // Get the character 
40              char CH = STR .charAt (index); // random character
 41              // 2.3 write verification code 
42 is              g.drawString (CH + "", width / I. 5 *, height / 2 );
 43 is          }
 44 is  
45          // 2.4 Videos interference line 
46 is          G. the setColor (Color.green);
 47  
48          // randomly generated coordinate point 
49  
50         for ( int I = 0; I <. 6; I ++ ) {
 51 is              int X1 = ran.nextInt (width);
 52 is              int X2 = ran.nextInt (width);
 53 is  
54 is              int Y1 = ran.nextInt (height);
 55              int = Y2 ran.nextInt (height);
 56 is              g.drawLine (X1, Y1, X2, Y2);   // Videos interference line 
57          }
 58  
59  
60          // 3 outputs the image to show the page: the response character stream by the image output to the browser 
61 is          ImageIO.write (image, "JPG" , response.getOutputStream ());
 62 is      }
 63 is 
64     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
65         this.doPost(request, response);
66     }
67 }

 

 The definition of a web page, you can view the verification code, you can refresh the verification code.

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>验证码</title>
 6 
 7     <script>
 8         /*
 9                 点击超链接或者图片,需要换一张
10                 1.给超链接和图片绑定单击事件
11 
12                 2.重新设置图片的src属性值
13 
14          */
15         window.onload = function(){
16             //1.获取图片对象
17             var img = document.getElementById("checkCode");
18             //2.绑定单击事件
19             img.onclick = function(){
20                 //加时间戳
21                 var date = new Date().getTime();
22 
23                 img.src = "/web/checkCodeServlet?"+date;
24             }
25 
26             // 获取超链接对象
27             var a = document.getElementById("change");
28             
29             a.onclick = img.onclick();
30             
31         }
32 
33 
34     </script>
35 
36 
37 </head>
38 <body>
39 
40 
41 <img id="checkCode" src="/web/checkCodeServlet" />
42 
43 <a id="change" href="javascript:;">看不清换一张?</a>
44 
45 </body>
46 </html>

 

 效果图

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11620996.html