Kaptcha is a simple and convenient verification code generation tool.

Source: Internet
Author: User

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.

Kaptcha can be configured with the following information:

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 !)

Verification Code image size, border, border width, border color

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)

......

For more information, see the following web. xml file.

The following describes the usage:

1. first go to the official website to download jar: http://code.google.com/p/kaptcha/

2. Create a web project and import the kaptcha-2.3.jar to environment variables.

3. Configure the Web. xml file

It is actually configuring com. Google. Code. kaptcha. servlet. kaptchaservlet.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" <br/> Version = "2.4"> <br/> <servlet-Name> kaptcha </servlet-Name> <br/> <servlet-class> COM. google. code. kaptcha. servlet. kaptchaservlet </servlet-class> <br/> <init-param> <br/> <description> border around kaptcha. legal values are yes or no. </description> <br/> <param-Name> kaptcha. border </param-Name> <br/> <param-value> NO </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> color of the border. legal values are R, G, B (and optional alpha) or white, black, blue. </description> <br/> <param-Name> kaptcha. border. color </param-Name> <br/> <param-value> Red </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> thickness of the border around kaptcha. legal values are> 0. </description> <br/> <param-Name> kaptcha. border. thickness </param-Name> <br/> <param-value> 5 </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> width in pixels of the kaptcha image. </description> <br/> <param-Name> kaptcha. image. width </param-Name> <br/> <param-value> 80 </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> height in pixels of the kaptcha image. </description> <br/> <param-Name> kaptcha. image. height </param-Name> <br/> <param-value> 40 </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the image producer. </description> <br/> <param-Name> kaptcha. producer. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. impl. defaultkaptcha </param-value> <br/> </init-param> <br/> <description> the text producer. </description> <br/> <param-Name> kaptcha. textproducer. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. text. impl. defaulttextcreator </param-value> <br/> </init-param> <br/> <description> the characters that will create the kaptcha. </description> <br/> <param-Name> kaptcha. textproducer. char. string </param-Name> <br/> <param-value> abcde2345678gfynmnpwx </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the number of characters to display. </description> <br/> <param-Name> kaptcha. textproducer. char. length </param-Name> <br/> <param-value> 5 </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> A List Of comma separated font names. </description> <br/> <param-Name> kaptcha. textproducer. font. names </param-Name> <br/> <param-value> Arial, courier </param-value> <br/> </init-param> <br/> <description> the size of the font to use. </description> <br/> <param-Name> kaptcha. textproducer. font. size </param-Name> <br/> <param-value> 23 </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the color to use for the font. legal values are R, G, B. </description> <br/> <param-Name> kaptcha. textproducer. font. color </param-Name> <br/> <param-value> black </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the noise producer. </description> <br/> <param-Name> kaptcha. noise. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. impl. nonoise </param-value> <br/> </init-param> <br/> <description> the noise color. legal values are R, G, B. </description> <br/> <param-Name> kaptcha. noise. color </param-Name> <br/> <param-value> black </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the obscurificator implementation. </description> <br/> <param-Name> kaptcha. obscurificator. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. impl. shadowgimpy </param-value> <br/> </init-param> <br/> <description> the background implementation. </description> <br/> <param-Name> kaptcha. background. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. impl. defaultbackground </param-value> <br/> </init-param> <br/> <description> ending background color. legal values are R, G, B. </description> <br/> <param-Name> kaptcha. background. clear. to </param-Name> <br/> <param-value> White </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the word Renderer implementation. </description> <br/> <param-Name> kaptcha. word. impl </param-Name> <br/> <param-value> COM. google. code. kaptcha. text. impl. defaultwordrenderer </param-value> <br/> </init-param> <br/> <description> the value for the kaptcha is generated and is put into the httpsession. this is the key value for that item in the session. </description> <br/> <param-Name> kaptcha. session. key </param-Name> <br/> <param-value> kaptcha_session_key </param-value> <br/> </init-param> <br/> <init- param> <br/> <description> the date the kaptcha is generated is put into the httpsession. this is the key value for that item in the session. </description> <br/> <param-Name> kaptcha. session. date </param-Name> <br/> <param-value> kaptcha_session_date </param-value> <br/> </init-param> <br/> </Servlet> <br/> <servlet-mapping> <br/> <servlet-Name> kaptcha </servlet-Name> <br/> <URL-pattern>/kaptcha.jpg </URL -Pattern> <br/> </servlet-mapping> <br/> <welcome-file-List> <br/> <welcome-File> kaptchaexample. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app> <br/>

 

4. Write kaptchaexample. jsp

Jquery is used here, so the jquery library is added.

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> <br/> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "> <br/> <title> kaptcha example </title> <br/> <MCE: script Type = "text/JavaScript" src = "JS/jquery-1.3.2.js" mce_src = "JS/jquery-1.3.2.js"> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br /> Enter in the <br/> <a href = "http://code.google.com/p/kaptcha/" mce_href = "http://code.google.com/p/kaptcha/"> kaptcha </a> to see if it <br/> matches what is stored in the session attributes. <br/> <Table> <br/> <tr> <br/> <TD> <br/> <br/> <MCE: script Type = "text/JavaScript"> <! -- <Br/> $ ('# kaptchaimage '). click (<br/> function () {<br/> $ (this ). hide (). ATTR ('src', <br/> 'kaptcha.jpg? '+ Math. floor (math. random () * 100 )). fadein (); <br/>}) <br/> // --> </MCE: SCRIPT> <br/> click Change image <br/> </TD> <br/> <TD valign = "TOP"> <br/> <form method = "Post"> <br/> Verification Code:: <br/> <input type = "text" name = "kaptchafield"> <br/> <input type = "Submit" name = "Submit "> <br/> </form> <br/> </TD> <br/> </tr> <br/> </table> <br/> <br /> <br/> <% <br/> string C = (String) session <br/>. getattribute (COM. google. code. kaptcha. constants. kaptcha_session_key); <br/> string parm = (string) request. getparameter ("kaptchafield"); </P> <p> system. out. println (c); <br/> out. println ("parameter:" + parm + "? Session Key: "+ C +": "); <br/> If (C! = NULL & parm! = NULL) <br/>{< br/> If (C. equals (parm) <br/>{< br/> out. println ("<B> true </B>"); <br/>} else <br/> {<br/> out. println ("<B> false </B> "); <br/>}< br/>%> <br/> </body> <br/> </ptml> <br/>

5. Run the test

 

Project download: http://download.csdn.net/source/2687960

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.