kaptcha 简单例子 与 产生中文字符
kaptcha 是一个产生 随机 字符的 java框架,用于输入 验证 用了工厂模式与 , 装饰者,
现在提供一个 产生中文的 验证码 只要 产生字符的 类
ChineseText extends Configurable implements TextProducer
实现 getText 方法, Configurable 类中的 getConfig 获得 配置信息 Config 类的信息
比如 产生字符的长度 getConfig 中的 getTextProducerCharLength 方法,
下面是 kaptcha 框架 的 getTextProducerCharLength 方法
public int getTextProducerCharLength() { String paramName = "kaptcha.textproducer.char.length"; String paramValue = this.properties.getProperty(paramName); return this.helper.getPositiveInt(paramName, paramValue, 5); }
2 写一个 现在提供一个 产生中文的 验证码 只要 产生字符的 类 ChineseText
package com.nbw.common.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()); } }
3 写一个 web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns: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"> <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>com.nbw.common.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>500</param-value> </init-param> <init-param> <param-name>kaptcha.image.height</param-name> <param-value>300</param-value> </init-param> <init-param> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>90</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>KaptchaExample.jsp</welcome-file> </welcome-file-list> </web-app>
4 前台展示的 页面KaptchaExample.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Kaptcha Example</title> <script type="text/javascript"> </script> </head> <body> <h2>Kaptcha Example</h2> <% String c = (String) session .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String parm = (String) request.getParameter("kaptchafield"); out.println("Parameter: " + parm + " - Session Key: " + c + " : "); if (c != null && parm != null) { if (c.equals(parm)) { out.println("<b>true</b>"); } else { out.println("<b>false</b>"); } } %> <table> <tr> <td> <img src="Kaptcha.jpg" style="width:150px;height:30px" alt="看不清楚,请点击图片" title="看不清楚,请点击图片"> </td> </tr> <tr> <td valign="top"> <form method="POST"> <br> sec code: <input type="text" name="kaptchafield"> <br /> <input type="submit" name="submit"> </form> </td> </tr> </table> </body> </html>
5 good luck !