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); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>Kaptcha</servlet-name> <!-- <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> --> <servlet-class>com.xhc.kaptchaServlet.MyKaptchaServlet</servlet-class> <!-- For a complete list of Init Parameters, please see: http://code.google.com/p/kaptcha/wiki/ConfigParameters --> <!-- 验证码边界,合法值yes or no --> <init-param> <param-name>kaptcha.border</param-name> <param-value>yes</param-value> </init-param> <!-- 边界颜色,合法值 r,g,b (and optional alpha) or white,black,blue --> <init-param> <param-name>kaptcha.border.color</param-name> <param-value>black</param-value> </init-param> <!-- 边界厚度,合法值 大于0 --> <init-param> <param-name>kaptcha.border.thickness</param-name> <param-value>1</param-value> </init-param> <!-- 验证码图片宽度 --> <init-param> <param-name>kaptcha.image.width</param-name> <param-value>200</param-value> </init-param> <!-- 验证码图片高度 --> <init-param> <param-name>kaptcha.image.height</param-name> <param-value>50</param-value> </init-param> <!-- 验证码图片生成类,默认由DefaultKaptcha类生成,可以自己写一个图片生成类,需要实现Producer接口并继承Configurable 类 --> <init-param> <param-name>kaptcha.producer.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultKaptcha</param-value> </init-param> <!-- 验证码生成类,默认由DefaultTextCreator类生成,可以自己写一个验证码生成类,需要实现TextProducer接口并继承Configurable 类 --> <init-param> <param-name>kaptcha.textproducer.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value> </init-param> <!-- 用来生成验证码的字符集 --> <init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>01234565789</param-value> </init-param> <!-- 生成验证码的长度 --> <init-param> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>4</param-value> </init-param> <!-- 验证码字体,多个值用逗号隔开 --> <init-param> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>Arial,Courier</param-value> </init-param> <!-- 字体大小 --> <init-param> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>40</param-value> </init-param> <!-- 字体颜色 --> <init-param> <param-name>kaptcha.textproducer.font.color</param-name> <param-value>blue</param-value> </init-param> <!-- 验证码字符间隔 --> <init-param> <param-name>kaptcha.textproducer.char.space</param-name> <param-value>5</param-value> </init-param> <!-- 图片干扰生成类,默认由DefaultNoise类生成,可以自己写一个图片干扰类,需要实现NoiseProducer接口并继承Configurable 类 --> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value> </init-param> <!-- 干扰颜色 --> <init-param> <param-name>kaptcha.noise.color</param-name> <param-value>yellow</param-value> </init-param> <!--获取扭曲的不同风格的验证码,官方实现的有ShadowGimpy,FishEyeGimpy,WaterRipple,其中用来FishEyeGimpy之后上面设定的干扰线不起作用--> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.FishEyeGimpy</param-value> </init-param> <!--渐变背景色,初始颜色 --> <init-param> <param-name>kaptcha.background.clear.from</param-name> <param-value>gray</param-value> </init-param> <!-- 渐变北京市,最终颜色 --> <init-param> <param-name>kaptcha.background.clear.to</param-name> <param-value>white</param-value> </init-param> <!-- 文字渲染器 --> <init-param> <param-name>kaptcha.word.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value> </init-param> <init-param> <param-name>kaptcha.session.key</param-name> <param-value>KAPTCHA_SESSION_KEY</param-value> </init-param> <init-param> <param-name>kaptcha.session.date</param-name> <param-value>KAPTCHA_SESSION_DATE</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/Kaptcha.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>KaptchaExample.jsp</welcome-file> </welcome-file-list> </web-app>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sinat_23128227/article/details/46897341