"To generate a verification code using Kaptcha

Source: Internet
Author: User

Original blog from: http://www.cnblogs.com/xdp-gacl/p/4221848.html thanks!

Kaptcha is a simple and easy-to-use code generation tool that allows you to define CAPTCHA size, color, displayed characters, and more through configuration. Here's how to use Kaptcha to generate a verification code and check out the verification code on the server side.

first, build the test environment1.1. Create a Web test project

Create a new Web project and place Kaptcha-2.3.2.jar in the project's Web-inf/lib directory, as shown in:

  

1.2. Kaptchaservlet to generate verification code in Web. xml file Configuration

The detailed configuration of the Kaptchaservlet is as follows:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "2.5" 3 xmlns= "http://java.sun.com/xml/ns/javae E "4 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "5 xsi:schemalocation=" Http://java.sun.com/xml/ns/java EE 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> 7 <welcome-file-list> 8 <welcome-file>i          Ndex.jsp</welcome-file> 9 </welcome-file-list>10 <!--kaptcha Verification code configuration-->12 <servlet>13 <!--servlet-->14 <servlet-name>kaptcha</servlet-name>15 to generate images <servlet-clas         S>com.google.code.kaptcha.servlet.kaptchaservlet</servlet-class>16 <!--is there a border-->18 <init-param>19 <param-name>kaptcha.border</param-name>20 <param-value>n             O</param-value>21 </init-param> <!--font color-->23 <init-param>24 <param-name> kaptcha.textproducer.font.color</param-name>25 <param-value>red</param-value>26 &lt ;/init-param>27 <!--picture width-->28 <init-param>29 <param-name>kaptcha.image.         Width</param-name>30 <param-value>135</param-value>31 </init-param>32 <!--which characters are used to generate the captcha-->33 <init-param>34 &LT;PARAM-NAME&GT;KAPTCHA.TEXTPRODUCER.CHAR.STRING&L          T;/param-name>35 <param-value>acdefhkprstwx345679</param-value>36 </init-param>37 <!--picture height-->38 <init-param>39 <param-name>kaptcha.image.height</param-n ame>40 <param-value>50</param-value>41 </init-param>42 <!--font size--&GT             ; <init-param>44 <param-name>kaptcha.textproducer.font.size</param-name>45 <param-value>43</param-value>46 </init-param>47 <!--interference Line Color-->48 <init-param>49 <param-name>kaptcha.noise.color</param-name>50 <param-value>black</param-value>5 1 </init-param>52 <!--number of characters-->53 <init-param>54 <param-name>k Aptcha.textproducer.char.length</param-name>55 <param-value>4</param-value>56 </i nit-param>57 <!--which fonts are used-->58 <init-param>59 <param-name>kaptcha.textpro Ducer.font.names</param-name>60 <param-value>arial</param-value>61 &LT;/INIT-PARAM&G        T </servlet>63 <!--map URL-->64 <servlet-mapping>65 <servlet-name>kaptcha&lt ;/servlet-name>66 <url-pattern>/kaptcha.jpg</url-pattern>67 </servlet-mapping>68 </web -app>
1.3, display the generated verification code

Show verification Code on page index.jsp

 1 <%@ page language= "java" pageencoding= "UTF-8"%> 2 <! DOCTYPE html> 3 

The effect is as follows:

  

1.4, the verification code submitted on the server-side verification

After the user submits the verification code in the Form form, we validate it on the server side and write a verifyservlet with the following code:

 1/** 2 * 3 */4 package Me.gacl.web.controller; 5 6 Import Java.io.IOException; 7 Import Java.io.PrintWriter; 8 9 Import javax.servlet.servletexception;10 import javax.servlet.http.httpservlet;11 Import JAVAX.SERVLET.HTTP.HTTPSERVLETREQUEST;12 Import javax.servlet.http.httpservletresponse;13 public class Verifyservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse resp Onse) throws Servletexception, IOException {response.setcontenttype ("Text/html;charaset=utf-8")         ); Response.setheader ("Pragma", "no-cache"); Response.setheader ("Cache-control", "No-cache"); 23 PrintWriter out = null;24 try {25///Response data-String resultdata;27//Get pass-through verification code 2 8 String Verifycode = Request.getparameter ("Verifycode"), System.out.println ("verifycode----" +ve Rifycode), if (verifycode== "") {Resultdata = "N";}else {33//Get Kaptcha Generate verification code stored in session String Kaptchavalue = (stri ng) Request.getsession (). getattribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); 35//Compare the input verification code And the actual generated verification code is the same if (Kaptchavalue = = NULL | | kaptchavalue = = "" | |!                     Verifycode.equalsignorecase (Kaptchavalue)) {PNs Resultdata = "N";}else {39 Resultdata = "Y";}41}42 out = Response.getwriter ();         Ut.write (Resultdata); Out.flush ();}catch (Exception e) {e.printstacktrace (); 47 }finally {$ if (out! = null) {out.close (); 50}51}52}53}

Registering Verifyservlet in Web. xml

1 <!--Check that the verification code is entered correctly-->2     <servlet>3         <servlet-name>verifyservlet</servlet-name>4         <servlet-class>me.gacl.web.controller.verifyservlet</servlet-class>5     </servlet>6     <servlet-mapping>7         <servlet-name>verifyservlet</servlet-name>8         <url-pattern >/servlet/verifyservlet</url-pattern>9     </servlet-mapping>

The results of the operation are as follows:

1, the Verification code does not enter

  

2. Enter the wrong verification code

  

3. Enter the correct verification code

  

Using Kaptcha to generate verification code feel very good, very useful and convenient.

"To generate a verification code using Kaptcha

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.