使用方法就是:
先下载http://code.google.com/p/kaptcha/下载
把kaptcha-2.3.jar把文件到 WEB-INF/lib目录下
web.xml配置信息
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>kaptcha</display-name> <servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!-- 设置是否有边框 --> <init-param> <param-name>kaptcha.border</param-name> <param-value>yes</param-value> </init-param> <!-- 设置边框颜色--> <init-param> <param-name>kaptcha.border.color</param-name> <param-value>105,179,90</param-value> </init-param> <!-- 获取中文 --> <init-param> <param-name>kaptcha.textproducer.impl</param-name> <param-value>org.cric.util.ChineseText</param-value> </init-param> <!-- 设置字体颜色 --> <init-param> <param-name>kaptcha.textproducer.font.color</param-name> <param-value>black</param-value> </init-param> <!-- 设置验证码宽度 --> <init-param> <param-name>kaptcha.image.width</param-name> <param-value>130</param-value> </init-param> <!-- 设置验证码高度 --> <init-param> <param-name>kaptcha.image.height</param-name> <param-value>44</param-value> </init-param> <!-- 设置字体大小 --> <init-param> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>30</param-value> </init-param> <!-- 设置字体个数 --> <init-param> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>4</param-value> </init-param> <!-- 设置字体样式 --> <init-param> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>宋体,楷体,微软雅黑</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/kaptcha.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
这个插件自带了个中文生成类,但是这个类做得很死,中文是手动输入进去的,我的天,太死了,然后leo就找啊找,发现API中有一个自定义接口,哈哈,太好了,正好我之前有一个生成中文的程序,直接COPY过来,写了个简单的类,测试,成功了,就是上面发的图。接口是这个
package org.cric.util; import java.util.Random; import com.google.code.kaptcha.text.TextProducer; import com.google.code.kaptcha.util.Configurable; /** * 随机产生字符,包括汉字 length 表示 长度 * @author qcy * */ public class ChineseText extends Configurable implements TextProducer{ public String getText() { int length = getConfig().getTextProducerCharLength(); //int length = 4; String finalWord="",firstWord=""; int tempInt=0; String[] array={ "0","1","2","3", "4","5","6","7", "8","9","a","b", "c","d","e","f"}; Random rand = new Random(); for(int i=0;i<length;i++){ switch(rand.nextInt(3)){ case 1: tempInt=rand.nextInt(26)+65; firstWord=String.valueOf((char)tempInt); break; case 2: int r1,r2,r3,r4; String strH,strL;// high&low r1=rand.nextInt(3)+11; // 前闭后开[11,14) if(r1==13){ r2=rand.nextInt(7); }else{ r2=rand.nextInt(16); } r3=rand.nextInt(6)+10; if(r3==10){ r4=rand.nextInt(15)+1; }else if(r3==15){ r4=rand.nextInt(15); }else{ r4=rand.nextInt(16); } strH=array[r1]+array[r2]; strL=array[r3]+array[r4]; byte[] bytes=new byte[2]; bytes[0]=(byte)(Integer.parseInt(strH,16)); bytes[1]=(byte)(Integer.parseInt(strL,16)); firstWord=new String(bytes); break; default: tempInt=rand.nextInt(10)+48; firstWord=String.valueOf((char)tempInt); break; } finalWord+=firstWord; } return finalWord; } public static void main(String[] args) { // TODO Auto-generated method stub ChineseText ct = new ChineseText(); System.out.print(ct.getText()); } }
Java中获取真实的验证码:
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
前台页面:
<img src="kaptcha.jpg" />