亨利·黑兹利特:kaptcha简单的验证码生成工具
来源:百度文库 编辑:偶看新闻 时间:2024/05/05 19:43:16
kaptcha是一个非常实用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的。
kaptcha工作的原理是调用com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到HttpSession中。
kaptcha可以配置一下信息:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)
验证码的样式(鱼眼样式、3D、普通模糊……当然也可以继承com.google.code.kaptcha.GimpyEngine自定义样式)
……
详细信息请看下面的web.xml文件
下面介绍一下用法:
1.首先去官网下载jar:http://code.google.com/p/kaptcha/
2.建立一个web项目,导入
3.配置web.xml文件
其实就是配置com.google.code.kaptcha.servlet.KaptchaServlet
Java代码:
version="2.4">
optional alpha) or white,black,blue.
> 0.
the HttpSession. This is the key value for that item in the session.
HttpSession. This is the key value for that item in the session.
4.编写KaptchaExample.jsp
这里用到了jQuery,所以添加了jQuery的库
js代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
Enter in the
单击换图片
<%
String c = (String) session
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String parm = (String) request.getParameter("kaptchafield");
System.out.println(c);
out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");
if (c != null && parm != null)
{
if (c.equals(parm))
{
out.println("true");
} else
{
out.println("false");
}
}
%>