Verification codes are widely used in various scenarios, the random code function is added to the new user account registration, user logon, website unified login, or the user publishes an article or content in the website information module in a variety of systems or software, the verification code is used to prevent the abuse of automatically registered or published programs on the network. The principle of the Verification Code is to randomly select some verification codes to display the verification code in the form of images on the software verification interface or verification page, when submitting an image, you must enter and submit the verification code on the image. If the submitted verification code is the same as the verification code saved by the server session, the submitted information is considered valid, otherwise, submission is rejected. When using the verification code, in order to avoid the automatic analysis program to parse the image and obtain the verification information, it is usually necessary to randomly generate some interference lines on the image or distort and blur the image with complicated characters, this increases the difficulty of automatically identifying programs to analyze and verify images.
JSP verification code source code Daquan will be used to introduce the implementation of several verification codes in JSP source code and use.
I. source code of the digital verification code generated in JSP
A digital verification code is the most common form of verification Verification code. The following is the JSP source code for digital implementation:
Num. jsp <% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%> <%! Color getRandColor (int cc, int bb) { Random random = new Random (); If (fc> 255) cc = 255; If (bc> 255) bb = 255; Int r = cc + random. nextInt (bb-cc ); Int g = cc + random. nextInt (bb-cc ); Int B = cc + random. nextInt (bb-cc ); Returnnew Color (r, g, B ); } // Obtain the random color bkjia.com %> <% Response. setHeader ("Pragma", "No-cache "); Response. setHeader ("Cache-Control", "no-cache "); Response. setDateHeader ("Expires", 0 ); Int width = 80; // defines the length of the Verification Code image. Int height = 30; // defines the image width of the verification code. BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB ); Graphics g = image. getGraphics (); Random random = new Random (); G. setColor (getRandColor (200,250 )); G. fillRect (0, 0, width, height ); G. setFont (new Font ("Times New Roman", Font. PLAIN, 18 )); // Define the font format bkjia.com G. setColor (getRandColor (160,200 )); For (int I = 0; I <155; I ++) { Int I _x = random. nextInt (width ); Int I _y = random. nextInt (height ); Int I _xl = random. nextInt (12 ); Int I _yl = random. nextInt (12 ); G. drawLine (I _x, I _y, I _x + I _xl, I _y + I _yl ); } // Draw the background with a line bar String s_Rand = ""; For (int I = 0; I <4; I ++) { String rand = String. valueOf (random. nextInt (10 )); S_Rand + = rand; G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 ))); G. drawString (rand, 13 * I + 6, 16 ); } // Generates four random Codes Session. setAttribute ("rand", s_Rand ); // Save the verification code to the Session G. dispose (); ImageIO. write (image, "JPEG", response. getOutputStream ()); // Output verification image bkjia.com Out. clear (); Out = pageContext. pushBody (); %> |
Next, the article will introduce the implementation of the Chinese verification code and other JAVA verification codes in JSP, as well as the call and use of the verification code in JSP. The next article will introduce the source code implementation of the JSP Chinese verification code.