1.新建一个Dynamic Web Project项目,里面新建一个servlet文件,还有两个jsp文件和一个xml文件,在lib里面加入要用到的jar包,然后导入。整体的框架图如下所示:
2.KaptchaServlet.java里面的代码如下所示:
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.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;
public class KaptchaServlet extends HttpServlet implements Servlet {
private Properties props;
private Producer kaptchaProducer;
private String sessionKeyValue;
public KaptchaServlet() {
this.props = new Properties();
this.kaptchaProducer = null;
this.sessionKeyValue = 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();
}
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 s1 = capText.substring(0, 1);
String s2 = capText.substring(1, 2);
int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue();
req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));
BufferedImage bi = this.kaptchaProducer.createImage(s1 + "+" + s2 + "=?");
ServletOutputStream out = resp.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
randomcode
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
check
<%
String k = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String str = request.getParameter("r");
if (k.equals(str)){
out.print("验证成功!
");
}else{
out.print("验证失败!
");
}
out.print("正确的结果是:" + k + "
");
out.print("你输入的结果是:" + str + "
");
%>
Kaptcha
KaptchaServlet
图片边框,合法值:yes , no
kaptcha.border
yes
边框颜色,合法值: r,g,b (and optional alpha) 或者
white,black,blue.
kaptcha.border.color
black
边框厚度,合法值:>0
kaptcha.border.thickness
1
图片宽 200
kaptcha.image.width
200
图片高 50
kaptcha.image.height
50
图片实现类
kaptcha.producer.impl
com.google.code.kaptcha.impl.DefaultKaptcha
文本实现类
kaptcha.textproducer.impl
com.google.code.kaptcha.text.impl.DefaultTextCreator
文本集合,验证码值从此集合中获取
kaptcha.textproducer.char.string
1234567890
验证码长度 5
kaptcha.textproducer.char.length
2
字体 Arial, Courier
kaptcha.textproducer.font.names
Arial, Courier
字体大小 40px.
kaptcha.textproducer.font.size
40
字体颜色,合法值: r,g,b 或者 white,black,blue.
kaptcha.textproducer.font.color
black
文字间隔 2
kaptcha.textproducer.char.space
2
干扰实现类
kaptcha.noise.impl
com.google.code.kaptcha.impl.DefaultNoise
干扰颜色,合法值: r,g,b 或者 white,black,blue.
kaptcha.noise.color
black
图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple
鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
阴影com.google.code.kaptcha.impl.ShadowGimpy
kaptcha.obscurificator.impl
com.google.code.kaptcha.impl.WaterRipple
背景实现类
kaptcha.background.impl
com.google.code.kaptcha.impl.DefaultBackground
背景颜色渐变,开始颜色
kaptcha.background.clear.from
green
背景颜色渐变,结束颜色
kaptcha.background.clear.to
white
文字渲染器
kaptcha.word.impl
com.google.code.kaptcha.text.impl.DefaultWordRenderer
session中存放验证码的key键
kaptcha.session.key
KAPTCHA_SESSION_KEY
The date the kaptcha is generated is put into the
HttpSession. This is the key value for that item in the
session.
kaptcha.session.date
KAPTCHA_SESSION_DATE
Kaptcha
/randomcode.jpg
index.jsp