1. 需要的jar包: patchca.jar: http://code.google.com/p/patchca/
patchca使用LGPL协议: GNU Lesser General Public License
2. 请求验证码的 JSP: validate.jsp
3. 产生验证码的 Servlet: ValidationCode.java
4. 验证验证码的 Servlet: ValidateCode.java
验证过程: validate.jsp请求验证码 -> ValidationCode.java生成验证码图片和相应的字符给validate.jsp -> 用户输入验证码 -> ValidateCode.java验证用户的验证码是否输入正确.
================================== validate.jsp ==================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img src="ValidationCode">
<form action="ValidateCode" method="post">
<input type="text" name="validateCode"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
================================== ValidateCode.java ==================================
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Validate
*/
public class ValidateCode extends HttpServlet {
private static final long serialVersionUID = 1L;
public ValidateCode() {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
String validateCode = request.getParameter("validateCode");
HttpSession session = request.getSession();
// 从session中取得验证码与用户输入的验证码进行比较
String storedValidateCode = (String) session.getAttribute(session.getId());
response.setHeader("Content-type", "text/html; charset=UTF-8");
if (validateCode.equalsIgnoreCase(storedValidateCode)) {
response.getWriter().println("验证码输入正确: " + validateCode);
} else {
response.getWriter().println("验证码输入错误: " + validateCode);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
}
================================== ValidationCode.java ==================================
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
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;
import org.patchca.color.GradientColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.RippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;
/**
* Servlet implementation class ValidateCode
*/
public class ValidationCode extends HttpServlet {
private static final long serialVersionUID = 1L;
public ValidationCode() {
super();
}
// 关键之处,产生图片
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("image/png");
ValidateEntry ve = new ValidateEntry();
HttpSession session = request.getSession();
session.setAttribute(session.getId(), ve.getCode()); // 验证码保存在session中
ServletOutputStream out = response.getOutputStream();
BufferedImage img = ve.getImage();
ImageIO.write(img, "png", out);
out.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
protected class ValidateEntry {
String code;
BufferedImage image;
ValidateEntry() {
createEntry();
}
public String getCode() {
return code;
}
public BufferedImage getImage() {
return image;
}
private void createEntry() {
ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
cs.setColorFactory(new GradientColorFactory());
switch ((int) (System.currentTimeMillis() % 6)) {
case 0:
cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
break;
case 1:
cs.setFilterFactory(new MarbleRippleFilterFactory());
break;
case 2:
cs.setFilterFactory(new DoubleRippleFilterFactory());
break;
case 3:
cs.setFilterFactory(new WobbleRippleFilterFactory());
break;
case 4:
cs.setFilterFactory(new DiffuseRippleFilterFactory());
break;
case 5:
cs.setFilterFactory(new RippleFilterFactory());
break;
}
Captcha captcha = cs.getCaptcha();
code = captcha.getChallenge();
image = captcha.getImage();
}
}
}