Kaptcha is a very practical verification code generation tool. With it, you can generate verification codes of various styles because it is configurable. The working principle of kaptcha is to call com. google. code. kaptcha. servlet. KaptchaServlet to generate an image. At the same time, the generated verification code string is placed in HttpSession
1. Download kaptcha.
Http://code.google.com/p/kaptcha/downloads/list
2. Create a Web project and place the kaptcha-2.3.2.jar In the downloaded package under the project's WEB-INF/lib directory.
3. Configure the web. xml file.
With kaptcha, you can easily configure it:
• Font of the Verification Code
• Font size of the Verification Code
• Font color of the Verification Code font
• Verification Code content range (numbers, letters, Chinese characters !)
• Size, border, border width, and border color of the Verification Code Image
• Interference line of the Verification code (you can inherit com. google. code. kaptcha. NoiseProducer to write a custom interference line)
• Verification Code style (fish eye style, 3D, General Fuzzy ...... Of course, you can also inherit the custom style of com. google. code. kaptcha. GimpyEngine)
Configuration
| The Code is as follows: |
Copy code |
|
If kaptcha. border has a border, the default value is true. You can set yes, no.
Kaptcha. border. color border Color: color. BLACK by default
Kaptcha. border. thickness the border width is 1 by default.
Kaptcha. producer. impl Verification Code Generator default: DefaultKaptcha
Kaptcha. textproducer. impl Verification Code text generator defaults to defatexttextcreator
The content range of the kaptcha. textproducer. char. string Verification Code text character is abcde2345678gfynmnpwx by default.
Kaptcha. textproducer. char. length Verification Code text character length: 5 by default
Kaptcha. 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 by default
Kaptcha. textproducer. font. color Verification Code text Character Color: color. BLACK by default
Kaptcha. textproducer. char. space Verification Code text Character spacing is 2 by default
Kaptcha. noise. impl Verification Code noise generation object default: defanonoise
Kaptcha. noise. color Verification Code noise Color: color. BLACK by default
Kaptcha. obscurificator. impl Verification Code style engine default is WaterRipple
Kaptcha. word. impl Verification Code text character rendering default: DefaultWordRenderer
Kaptcha. background. impl Verification Code background generator default: DefaultBackground
Kaptcha. background. clear. from verification code background Color progressive default: Color. LIGHT_GRAY
Kaptcha. background. clear. to verification code background Color progressive default value: Color. WHITE
Kaptcha. image. width Verification Code the image width is 200 by default.
Kaptcha. image. height Verification Code image height: 50 by default |
Verification Code instance
| The Code is as follows: |
Copy code |
|
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Display-name> </display-name>
<! -- Kaptcha verification code configuration -->
<Servlet>
<! -- Generate the image Servlet -->
<Servlet-name> Kaptcha </servlet-name>
<Servlet-class> com. google. code. kaptcha. servlet. KaptchaServlet </servlet-class>
<! -- Whether a Border exists -->
<Init-param>
<Param-name> kaptcha. border </param-name>
<Param-value> no </param-value>
</Init-param>
<! -- Font color -->
<Init-param>
<Param-name> kaptcha. textproducer. font. color </param-name>
<Param-value> red </param-value>
</Init-param>
<! -- Image Width -->
<Init-param>
<Param-name> kaptcha. image. width </param-name>
<Param-value> 135 </param-value>
</Init-param>
<! -- Used to generate the verification code -->
<Init-param>
<Param-name> kaptcha. textproducer. char. string </param-name>
<Param-value> ACDEFHKPRSTWX345679 </param-value>
</Init-param>
<! -- Image Height -->
<Init-param>
<Param-name> kaptcha. image. height </param-name>
<Param-value> 50 </param-value>
</Init-param>
<! -- Font size -->
<Init-param>
<Param-name> kaptcha. textproducer. font. size </param-name>
<Param-value> 43 </param-value>
</Init-param>
<! -- Interference line color -->
<Init-param>
<Param-name> kaptcha. noise. color </param-name>
<Param-value> black </param-value>
</Init-param>
<! -- Number of characters -->
<Init-param>
<Param-name> kaptcha. textproducer. char. length </param-name>
<Param-value> 4 </param-value>
</Init-param>
<! -- Font used -->
<Init-param>
<Param-name> kaptcha. textproducer. font. names </param-name>
<Param-value> Arial </param-value>
</Init-param>
</Servlet>
<! -- Ing url -->
<Servlet-mapping>
<Servlet-name> Kaptcha </servlet-name>
<Url-pattern>/Kaptcha.jpg </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app> |
Index. jsp
| The Code is as follows: |
Copy code |
|
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Verification Code </title>
<Script type = "text/javascript">
// Click to switch the verification code
Function changeVerifyCode (img ){
Img. src = "Kaptcha.jpg? "+ Math. floor (Math. random () * 100 );
}
</Script>
</Head>
<Body>
<P>
<Form action = "result. jsp">
<Table>
<Tr>
<Td>
& Nbsp;
</Td>
<Td>
</Td>
</Tr>
<Tr>
<Td>
Enter the verification code:
</Td>
<Td>
<Input type = "text" name = "verifyCode">
</Td>
</Tr>
<Tr>
<Td>
& Nbsp;
</Td>
<Td>
<Input type = "submit" value = "submit">
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html> |
1. kaptcha provides good scalability, And the extension method is also very simple. You only need to implement the relevant interfaces, and then add our custom objects to the configuration items, such as Producer and WordRenderer, the interfaces GimpyEngine, BackgroundProducer, NoiseProducer, and TextProducer can be expanded at will.
2. kaptcha also provides many off-the-shelf filters for noise-based verification codes. The default WaterRipple generated by kaptcha is RippleFilter, waterFilter is composed of two filters, which implement various processing of text style changes, located at com. jhlabs. under image package