【www.bbyears.com--Google】
1.从官网https://code.google.com/p/kaptcha/下载kaptcha压缩文件,解压文件后里面有一个war文件,打开Eclipse/MyEclipse将其import进去,然后部署到服务器,在浏览器输入url即可看到kaptcha官方提供的基本demo的运行情况,现在将其改为加法计算验证。
2.首先查看web.xml文件发现用来生成验证码的servlet为KaptchaServlet
3.找到KaptchaServlet.class文件,然后进行反编译。
4.新建一个自己的验证码MyKaptchaServlet,将反编译得到的源码拷贝进来。
5.对MyKaptchaServlet做如下修改。
修改后MyKaptchaServlet.java代码如下:
package com.xhc.kaptchaServlet;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MyKaptchaServlet extends HttpServlet implements Servlet {
private Properties props;
private Producer kaptchaProducer;
private String sessionKeyValue;
private String sessionKeyDateValue;
public MyKaptchaServlet() {
this.props = new Properties();
this.kaptchaProducer = null;
this.sessionKeyValue = null;
this.sessionKeyDateValue = null;
}
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
ImageIO.setUseCache(false);
Enumeration initParams = conf.getInitParameterNames();
while (initParams.hasMoreElements()) {
String key = (String) initParams.nextElement();
String value = conf.getInitParameter(key);
this.props.put(key, value);
}
Config config = new Config(this.props);
this.kaptchaProducer = config.getProducerImpl();
this.sessionKeyValue = config.getSessionKey();
this.sessionKeyDateValue = config.getSessionDate();
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setDateHeader("Expires", 0L);
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
resp.setContentType("image/jpeg");
String capText = this.kaptchaProducer.createText();
String str1 = capText.substring(0, 2);
String str2 = capText.substring(2, 4);
int result = Integer.valueOf(str1) + Integer.valueOf(str2);
req.getSession().setAttribute(this.sessionKeyValue, result+"");
req.getSession().setAttribute(this.sessionKeyDateValue, new Date());
BufferedImage bi = this.kaptchaProducer.createImage(str1 + "+" + str2
+ "=?");
ServletOutputStream out = resp.getOutputStream();
ImageIO.write(bi, "jpg", out);
}
}
6.修改web.xml配置文件,将servlet-class指定到MyKaptchaServlet
参考官方文档
https://code.google.com/p/kaptcha/wiki/ConfigParameters将其他属性也配置进去,修改后web.xml文件如下:
version="2.4">
7.效果图
java自动生成验证码插件-kaptcha
kaptcha一个很好用的验证码插件,java版的,很不错的一个插件,只用简单的在web.xml中设置几个属性,一个很漂亮的验证码就出来了。其他的参数都可以自己设置,最牛的就是提供了接口,可以自己定义哦。
html页面代码
web.xml配置代码
java 代码
String kaptchaExpected = (String)request.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String kaptchaReceived = request.getParameter("kaptcha");
if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected))
{
setError("kaptcha", "Invalid validation code.");
}
点击显示代码 刷新方法 js方法
<script type="text/javascript">
$(function(){
$('#kaptchaImage').click(function () {
$(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) );
})
});
</script>
Can't read the image? Click it to get a new one.
其他参数设置