Use Kaptcha. jar to generate an image Verification Code (the following source code can be copied and customized ),

Source: Internet
Author: User

Use Kaptcha. jar to generate an image Verification Code (the following source code can be copied and customized ),

Note: Kaptcha is a very practical verification code generation tool that can generate verification codes of various styles, because it is a directory that can be configured: step 2: Compile jsp page B and configure web. xml C verifies whether the input is correct.

1. Implementation steps
1 download Kaptcha-2.3.jar
2. Copy the jar package and place it under the lib package of the project.
3. Compile the page
4. Configure web. xml
5. Generate and verify simple code
Example 2
A: Compile the jsp page <body> <form action = "LoginAction? Method = login "> This is the servlet method. The username of the Framework is <input type =" text "name =" username "/> password: <input type = "text" name = "password"/> Verification Code  click to refresh the image. Enter the verification code: <input type = "text" name = "inputCode"/> <input type = "submit"> </from> </body>
B. Configure web. xml (you can directly copy it in use)
<Servlet> <servlet-name> Kaptcha </servlet-name> <servlet-class> com. google. code. kaptcha. servlet. kaptchaServlet </servlet-class> more configurations are as follows (the name value remains unchanged, and the value can be replaced)
A. Configure whether the verification code image Displays the border
 < init-param> 
<Description> image border, valid values: yes, no </description>
 < param-name>kaptcha.border< /param-name> 
 < param-value>yes< /param-value> 
 < /init-param>
B. Configure the border color
 < init-param> 
<Description> border color. Valid values: black, white, blue, and other color words </description>
 < param-name>kaptcha.border.color< /param-name>
 < param-value>black< /param-value> 
 < /init-param> 
C. Configure the Border thickness.
<Init-param>
<Description> Border thickness, valid value:> 0 </description>
<Param-name> kaptcha. border. thickness </param-name>
<Param-value> 1 </param-value>
</Init-param>
D. Configure the image width of the verification code.
<Init-param>
<Description> Verification Code image width, valid value:> 0 </description>
<Param-name> kaptcha. image. width </param-name>
<Param-value> 200 </param-value>
</Init-param>
E. Configure the height of the Verification Code image <init-param> <description> Verification Code image. Valid value:> 0 </description> <param-name> kaptcha. image. height </param-name> <param-value> 50 </param-value> </init-param> f: Image configuration implementation class <init-param> <description> Image </description> <param-name> kaptcha. producer. impl </param-name> <param-value> com. google. code. kaptcha. impl. defaultKaptcha </param-value> </init-param> g configuration text implementation class <init-param> <descripti On> Implementation class of text </description> <param-name> kaptcha. textproducer. impl </param-name> <param-value> com. google. code. kaptcha. text. impl. defaultTextCreator </param-value> </init-param> h indicates that the verification code value contains letters, numbers, and data contained in the <init-param> <description> Verification Code. Valid values: letters, numbers, etc. </description> <param-name> kaptcha. textproducer. char. string </param-name> <param-value> abcde12345ABCDE </param-value> </init-param> h configures the number of digits of the Verification Code <init-param> <Description> Number of digits of the verification code> 0 </description> <param-name> kaptcha. textproducer. char. length </param-name> <param-value> 5 </param-value> </init-param> I configure the font of the Verification Code <init-param> <description> font of the Verification Code (write the required font name into value, valid values: Arial, Courier, etc. </description> <param-name> kaptcha. textproducer. font. names </param-name> <param-value> Arial, Courier </param-value> </init-param> j. Set the font size of the Verification Code <init-param> <description> The font size of the verification code is 40px </description> <param-name> kaptcha. textproducer. font. size </param-name> <param-value> 40 </param-value> </init-param> k. Set the font color of the Verification Code <init-param> <description> Verification code color., valid values: black, white, blue, etc. </description> <param-name> kaptcha. textproducer. font. color </param-name> <param-value> black </param-value> </init-param> l configure the verification code interval <init-param> <description> Verification code data interval 2, etc. </description> <param-na Me> kaptcha. textproducer. char. space </param-name> <param-value> 2 </param-value> </init-param> m configuration verification code interference implementation class <init-param> <description> interference implementation class </description> <param-name> kaptcha. noise. impl </param-name> <param-value> com. google. code. kaptcha. impl. defaultNoise </param-value> </init-param> n sets the interference color <init-param> <description> the interference line color. Valid values: r, g, B, or white, black, blue </description> <param-name> kaptcha. nois E. color </param-name> <param-value> black </param-value> </init-param> o configure the verification code image style <init-param> <description> Verification Code Image style: watermark com. google. code. kaptcha. impl. waterRipple fish eye com. google. code. kaptcha. impl. fishEyeGimpy shadow com. google. code. kaptcha. impl. shadowGimpy </description> <param-name> kaptcha. obscurificator. impl </param-name> <param-value> com. google. code. kaptcha. impl. waterRipple </param-value> </in It-param> p Configuration Verification Code background implementation class <init-param> <description> background implementation class </description> <param-name> kaptcha. background. impl </param-name> <param-value> com. google. code. kaptcha. impl. defaultBackground </param-value> </init-param> Configure the key of the q Verification Code stored in the session <init-param> <description> session </description> <param-name> kaptcha. session. key </param-name> <param-value> KAPTCHA_SESSION_KEY </param-value> </init -Param> </servlet> <servlet-mapping> <servlet-name> Kaptcha </servlet-name> <url-pattern>/randomcode.jpg </url-pattern> <! -- Here is the generated Verification Code image --> <servlet-mapping>
C login verification // obtain the generated verification code in the background: String verify = (String) request. getSession (). getAttribute (com. google. code. kaptcha. constants. KAPTCHA_SESSION_KEY); // receives the verification code entered by the user: String userVerify = request. getParameter ("here is the input Tag name attribute value of the input verification code"); // you can determine if (verify. equals (userVerify) {starts to judge whether the user name and password are correct and other operations} else {request. setAttribute ("verifyError", "Incorrect verification code! ") Return to the logon page (getDispatch... Or struts2 )}

Related Article

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.