Start by importing the appropriate jar packages:
1. If it is a MAVEN project, add the following code in your pom file to automatically download the jar package to your project:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2. If it is an ordinary Web project, download it on the Internet. Add this jar package to the Web's Lib directory and use it in the build path of the jar import.
Then the corresponding configuration in the Web, the code is as follows:
<servlet>
<!– Generating a picture servlet–>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<!– whether there is a border –>
<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 the verification code –>
<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>
<!– the color of the interference line –>
<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 are used –>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>Arial</param-value>
</init-param>
</servlet>
<!– Mapping of Url–>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/Kaptcha</url-pattern>
</servlet-mapping>
The code for the verification code in the front-end HTML or form in the JSP:
<div class= "Form-group" >
<label for= "J_captcha" class= "T" > Verification Code:</label>
<input id= "J_captcha" name= "J_captcha" type= "text" class= "Form-control x164 in" >
</div>
Here the src= "Kaptcha" is to jump to the corresponding servlet to load the verification code. Click the Refresh verification code is a JS to control. The script for JS is:
Click Switch Verification Code
function Changeverifycode (IMG) {
IMG.SRC = "Kaptcha?" +math.floor (Math.random () *100);
}
This is a JavaScript script. This enables the refresh verification code to be implemented.
Generate the corresponding verification code, then how do we use it? How to get verification code? How to make the verification code is not case-sensitive? These are controlled by the action in the background.
public void Login (HttpServletRequest request,httpservletresponse response,String Kachepa) throws ioexception{
string kaptchaexpected = ( String) request.getsession ()
. GetAttribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); here is to get the corresponding verification code
if (kachepa.equalsignorecase ( kaptchaexpected)//) { //Verification code is not case sensitive
Setjsonstring ("{\" success\ ": true}");
}else if (!kachepa.equalsignorecase (kaptchaexpected)) {
setjsonstring ("{\" success\ ": False,\" result\ ": \" The CAPTCHA is incorrect!) \"}");
}
this.outprintjsonstring (response, jsonstring);
}
In this way, the corresponding verification code is implemented.
Kaptcha implementation Verification code in Java Web