Kaptcha is a simple and easy to use the Verification Code generation tool, through configuration, you can define the code size, color, display characters and so on. Here's how to generate a CAPTCHA using Kaptcha and check out the captcha on the server side.
First, build test environment
1.1. Create a Web test project
Create a new Web project and place the Kaptcha-2.3.2.jar in the Web-inf/lib directory of your project, as shown in the following illustration:
1.2, in the Web.xml file configuration to generate verification code Kaptchaservlet
The detailed configuration of the Kaptchaservlet is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" xml Ns: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 "> <welcome-file-list> <welcome-file>index.jsp</
Welcome-file> </welcome-file-list> <!--kaptcha Verification code configuration--> <servlet> <!--a servlet to generate a picture--> <servlet-name>Kaptcha</servlet-name> <servlet-class> Com.google.code.kaptcha.servlet.kaptchaservlet</servlet-class> <!--have borders--> <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> <!--picture width--> <init-param> <param-nAme>kaptcha.image.width</param-name> <param-value>135</param-value> </init-param> <
!--which characters are used to generate Authenticode--> <init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>ACDEFHKPRSTWX345679</param-value> </init-param> <!--picture 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> <!-- which fonts to use--> <init-param> <param-name>kaptcha.textproducer.font.names</param-name> < Param-value>arial</param-value> </init-param> </servlet> <!--map URL--> <servlet-mapp Ing> <servlet-name>Kaptcha</servlet-name> <url-pattern>/Kaptcha.jpg</url-pattern> <
/servlet-mapping> </web-app>
1.3, display the generated verification code
Display the CAPTCHA code on the page index.jsp
<%@ page language= "java" pageencoding= "UTF-8"%> <! DOCTYPE html>
The effect is as shown in the following illustration:
1.4. Verify the submitted verification code on the server side
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:
/** * * * * * * Package me.gacl.web.controller;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Verifyservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Res
Ponse) throws Servletexception, IOException {response.setcontenttype ("text/html;charaset=utf-8");
Response.setheader ("Pragma", "no-cache");
Response.setheader ("Cache-control", "No-cache");
PrintWriter out = null;
try {//Response data String resultdata;
Gets the pass-through verification code String Verifycode = Request.getparameter ("Verifycode");
System.out.println ("verifycode----" +verifycode);
if (verifycode== "") {Resultdata = "N"; }else {//Get Kaptcha Generate validation code String Kaptchavalue = (string) request.getsession (). getattribute (
Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
Compare the input verification code with the actual generated verification codeif (Kaptchavalue = null | | kaptchavalue = = "" | |!
Verifycode.equalsignorecase (Kaptchavalue)) {resultdata = "N";
}else {resultdata = "Y";
} out = Response.getwriter ();
Out.write (Resultdata);
Out.flush ();
}catch (Exception e) {e.printstacktrace ();
}finally {if (out!= null) {out.close ();
}
}
}
}
Register Verifyservlet in Web.xml
<!--check that the verification code is entered correctly-->
<servlet>
<servlet-name>VerifyServlet</servlet-name>
<servlet-class>me.gacl.web.controller.VerifyServlet</servlet-class>
</servlet>
< servlet-mapping>
<servlet-name>VerifyServlet</servlet-name>
<url-pattern>/servlet /verifyservlet</url-pattern>
</servlet-mapping>
The results of the operation are as follows:
1, the Verification code does not enter
2, the input error verification code
3, enter the correct verification code
Use Kaptcha to generate the verification code it feels pretty good, very easy and convenient, I hope you can master.