kaptcha验证码使用

来源:互联网 发布:软件测试说明书 编辑:程序博客网 时间:2024/05/16 13:06

效果图:


官方地址:https://code.google.com/p/kaptcha/w/list

1、把下载的kaptcha-2.3.2.jar添加到lib中

2、配置web.xml增加servlet

[html] view plaincopyprint?
  1. <servlet>  
  2.         <servlet-name>Kaptcha</servlet-name>  
  3.         <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.         <servlet-name>Kaptcha</servlet-name>  
  7.         <url-pattern>/kaptcha.jpg</url-pattern>  
  8. </servlet-mapping>  

3、在jsp页面中

[html] view plaincopyprint?
  1. <form action="submit.action" method="post">  
  2.     <img src="kaptcha.jpg" id="kaptchaImage" /> <input type="text"  
  3.         name="kaptcha" value="" /> <input type="submit" name="submit"  
  4.         value="submit" />  
  5. </form>  
其中src="kaptcha.jpg"会被定位到servlet上

4、KaptchaServlet会把验证码设置到session中,可以如下方式获取

[html] view plaincopyprint?
  1. String kaptchaExpected = (String)request.getSession()  
  2.     .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  

5、如果是struts2的action,可以如下方式获取

[html] view plaincopyprint?
  1. String kaptchaExpected = (String)ActionContext.getContext().getSession()  
  2. .get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
6、如果想设置点击图片更换验证码,可以加上如下js,需要jquery

[html] view plaincopyprint?
  1. <script type="text/javascript">  
  2.     $(function(){  
  3.         $('#kaptchaImage').click(function () { $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); })  
  4.     });  
  5. </script>  

7、或者来点fade效果

[html] view plaincopyprint?
  1. <script type="text/javascript">  
  2.     $(function() {  
  3.         $('#kaptchaImage').click(  
  4.                 function() {  
  5.                     $(this).hide().attr('src',  
  6.                             'kaptcha.jpg?' + Math.floor(Math.random() * 100)).fadeIn();  
  7.                 });  
  8.     });  
  9. </script>  

8、验证码图片还有很多参数设置

设置方法,在web.xml的servlet中

[html] view plaincopyprint?
  1. <init-param>  
  2.     <param-name>kaptcha.border</param-name>  
  3.     <param-value>no</param-value>  
  4. </init-param>  
Constant描述默认值kaptcha.border图片边框,合法值:yes , noyeskaptcha.border.color边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.blackkaptcha.border.thickness边框厚度,合法值:>01kaptcha.image.width图片宽200kaptcha.image.height图片高50kaptcha.producer.impl图片实现类com.google.code.kaptcha.impl.DefaultKaptchakaptcha.textproducer.impl文本实现类com.google.code.kaptcha.text.impl.DefaultTextCreatorkaptcha.textproducer.char.string文本集合,验证码值从此集合中获取abcde2345678gfynmnpwxkaptcha.textproducer.char.length验证码长度5kaptcha.textproducer.font.names字体Arial, Courierkaptcha.textproducer.font.size字体大小40px.kaptcha.textproducer.font.color字体颜色,合法值: r,g,b  或者 white,black,blue.blackkaptcha.textproducer.char.space文字间隔2kaptcha.noise.impl干扰实现类com.google.code.kaptcha.impl.DefaultNoisekaptcha.noise.color干扰颜色,合法值: r,g,b 或者 white,black,blue.blackkaptcha.obscurificator.impl图片样式:
水纹com.google.code.kaptcha.impl.WaterRipple
鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
阴影com.google.code.kaptcha.impl.ShadowGimpycom.google.code.kaptcha.impl.WaterRipplekaptcha.background.impl背景实现类com.google.code.kaptcha.impl.DefaultBackgroundkaptcha.background.clear.from背景颜色渐变,开始颜色light greykaptcha.background.clear.to背景颜色渐变,结束颜色whitekaptcha.word.impl文字渲染器com.google.code.kaptcha.text.impl.DefaultWordRendererkaptcha.session.keysession keyKAPTCHA_SESSION_KEYkaptcha.session.datesession dateKAPTCHA_SESSION_DATE
9、

水纹效果


鱼眼效果


阴影效果


更多0
0 0
原创粉丝点击