1.导入jar包 如kaptchar.2.3.jar 2.添加配置文件
3.使用方法`
代码语言:javascript复制
function changeR(node){ // 用于点击时产生不同的验证码 node.src = "/randomcode.jpg?time=" new Date().getTime() ; }
代码语言:javascript复制<body>
<%
// 检查是否是正确的验证码
String k = (String) session
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String str = request.getParameter("r");
if (k.equals(str))
out.print("true");
out.print(k "---" str);
%>
</body>
4.说明 使用加法验证码需要注意(重写)
代码语言:javascript复制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;
import javax.servlet.http.HttpSession;
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();
//直接从验证码集合中选取四位,前两位作为加数1,后两位作为加数2 因此配置需要填写**4**位验证码
String s1 = capText.substring(0, 2);
String s2 = capText.substring(2, 4);
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();
}
}
}
使用文字验证码直接修改文本集合即可,也可以重写类 如下
代码语言:javascript复制` public String getText() {
int length = getConfig().getTextProducerCharLength();
//char[] charS = getConfig().getTextProducerCharString();
代码语言:javascript复制 String[] s = new String[]{"慕","课","网","教","程","验","证","码","实","例"};
Random rand = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < length; i ){
int ind = rand.nextInt(s.length);
sb.append(s[ind]);
}
return sb.toString();
}`