在项目中添加kaptchar的jar包,下载地址:http://code.google.com/p/kaptcha/(建议使用迅雷下载)
步骤二:
配置web.xml,详细配置如下(这里不是所有的配置,这里的参数还有很多可以酌情精简):
<!-- 配置验证码插件servlet类 --> <servlet> <servlet-name>kaptcha</servlet-name> <servlet-class> com.google.code.kaptcha.servlet.KaptchaServlet </servlet-class> <!-- 给验证码插件servlet类配置参数 --> <!-- 边框设置 yes no --> <init-param> <param-name>kaptcha.border</param-name> <param-value>no</param-value> </init-param> <!-- 边框颜色 颜色名称 例如:red blue black等 也可以是 RGB:105,179,90 --> <init-param> <param-name>kaptcha.border.color</param-name> <param-value>red</param-value> </init-param> <!-- 边框宽度 --> <init-param> <param-name>kaptcha.border.thickness</param-name> <param-value>5</param-value> </init-param> <!-- 产生验证码的字符源头 兼容中文的 --> <init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>2345共2354阿斯asdf蒂芬2345爬墙2345额问题了ewry空1254间的股fktiul份的y7u12354o私人254认可ry13254iu的身份报123告快乐</param-value> </init-param> <!-- 生成结果的字符个数 --> <init-param> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>5</param-value> </init-param> <!-- 生成结果的字符字体 --> <init-param> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>微软雅黑,宋体,楷体</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>2</param-value> </init-param> <!-- 干扰线 生成器 --> <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>BLACK</param-value> </init-param> <!-- 干扰线 背景渐变颜色 从左侧向右渐变 --> <init-param> <param-name>kaptcha.background.clear.from</param-name> <param-value>green</param-value> </init-param> <!-- 干扰线 背景渐变颜色 从右侧向左渐变 --> <init-param> <param-name>kaptcha.background.clear.to</param-name> <param-value>yellow</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> </servlet> <!-- 验证码请求名 映射 --> <servlet-mapping> <servlet-name>kaptcha</servlet-name> <url-pattern>/yzm</url-pattern> </servlet-mapping>
步骤三:
在jsp页面中添加图片标签,标签的src静态资源请求地址为上面配置好的请求名 yzm,具体代码如下:
<br> <hr> <br> <form action="kaptchatest" method="post"> <img title = "点我换一张" alt="" src="yzm" style="cursor:pointer;" onclick="changeVerifyCode();"> <input type="text" name="yzm"> <br> <input type="submit" value="测试验证码"> </form> <script type="text/javascript"> //点击切换验证码 function changeVerifyCode(){ img.src = "yzm?"+Math.floor(Math.random()*100); } </script>
package com.kaptcha.action; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class KaptchaServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置request编码方式 request.setCharacterEncoding("utf-8"); //获取表单提交参数 String yzm = request.getParameter("yzm") ; System.out.println(yzm); //从session中获取google kaptcha 插件生成的验证码 String googlecode = request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY).toString() ; System.out.println(googlecode); //比较验证码正确性 System.out.println(googlecode.equalsIgnoreCase(yzm)); //true } }
<servlet> <description></description> <servlet-name>KaptchaServlet</servlet-name> <servlet-class>com.kaptcha.action.KaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>KaptchaServlet</servlet-name> <url-pattern>/kaptchatest</url-pattern> </servlet-mapping>
GOOGLE kaptcha 验证码生成插件 servlet/jsp项目中使用步骤
原文地址:http://blog.csdn.net/responsecool/article/details/39295089