Java Verification Code kaptcha

Source: Internet
Author: User

Java Verification Code kaptcha
Brief Introduction:

Kaptcha is a verification code library extended from simplecaptcha. It is a very practical verification code generation tool in Java programming. We can use this tool to generate verification codes of various styles because it is configurable and can be customized as needed.

Working principle:

The working principle of kaptcha is to call com. google. code. kaptcha. servlet. KaptchaServlet to generate a verification code image, respond to the client, and put the generated real verification code string into HttpSession.

Usage (Dynamic Web Project ):

1. To use this tool (or plug-in), first you need to import the jar package of kaptcha in the project. I am using a kaptcha-2.3.2.jar.

2. Configure the Servlet of kaptcha in web. xml.

 
  
   Kaptcha
  
  
   com.google.code.kaptcha.servlet.KaptchaServlet
  
 
 
  
   Kaptcha
  
  
   /http://www.bkjia.com/uploads/allimg/140812/0405015c6-0.jpg
  
 
3. When Using jsp or html files, you only need to add an img tag to display the verification code.

 
Note: The src attribute of the img label does not have to be named like this, but its resource path must correspond to the Servlet configuration in web. xml, which must be consistent. 

 
  /http://www.bkjia.com/uploads/allimg/140812/0405015c6-0.jpg
 
After completing the preceding three steps, the verification code will be effective. The results are all default. In order to get a more beautiful and more compliant verification code, we can configure some parameters by ourselves.

For example, configure four initialization parameters in Servlet to set the height, width, and characters of the Verification Code image to be generated to be numbers, and remove the interference line of the background.

 
  
   
Kaptcha
  
  
   
Com. google. code. kaptcha. servlet. KaptchaServlet
  
  
  
   
    
Kaptcha. image. width
   
   
    
100
   
  
  
  
   
    
Kaptcha. image. height
   
   
    
50
   
  
  
  
   
    
Kaptcha. textproducer. char. string
   
   
    
0123456789
   
  
  
  
   
    
Kaptcha. noise. impl
   
   
    
Com. google. code. kaptcha. impl. NoNoise
   
  
 
 
  
   
Kaptcha
  
  
   
Http://www.bkjia.com/uploads/allimg/140812/0405015c6-0.jpg
  
 

There are many other configurations similar to this. Some common configuration options are listed here. You can search for them online if you want them to be more comprehensive.

Kaptcha. whether the border of border is set to true by default. We can set yes and no kaptcha by ourselves. border. the color border Color is color by default. BLACK kaptcha. border. the default width of the thickness border is 1 kaptcha. producer. the default impl verification code generator is defakapkaptcha kaptcha. textproducer. the default value of impl Verification Code text generator is defatexttextcreator kaptcha. textproducer. char. the content range of string Verification Code text characters is abcde2345678gfynmnpwx kaptcha by default. textproducer. char. length Verification Code text character length is 5 kaptcha by default. textproducer. font. names Verification Code text Font style default new Font ("Arial", 1, fontSize), new Font ("Courier", 1, fontSize) kaptcha. textproducer. font. size Verification Code text character size 40 kaptcha by default. textproducer. font. color Verification Code text character Color is color by default. BLACK kaptcha. textproducer. char. the space Verification Code text Character spacing is 2 kaptcha by default. noise. the default value of the noise generated by impl verification code is DefaultNoise kaptcha. noise. color Verification Code noise Color is color by default. BLACK kaptcha. obscurificator. the default impl Verification Code style engine is WaterRipple kaptcha. word. the default value of impl Verification Code text character rendering is DefaultWordRenderer kaptcha. background. the default value of the impl Verification Code background generator is DefaultBackground kaptcha. background. clear. from verification code background Color gradual default is Color. LIGHT_GRAY kaptcha. background. clear. to Verification Code background Color gradual default is Color. WHITE kaptcha. image. width Verification Code the image width defaults to 200 kaptcha. image. height Verification Code Image height: 50 by default
To obtain the real verification code in java code, you only need to obtain the value com. google. code. kaptcha. Constants. KAPTCHA_SESSION_KEY from HttpSession. For example, you can set the value in Servlet.

String vcode = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
The client (browser) enters the submitted verification code value in the form and compares it with the actual value we obtained from HttpSession to determine whether the user verification code is entered correctly or not.

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.