首页 > 其他 > 详细

图片验证码

时间:2016-05-03 23:42:30      阅读:256      评论:0      收藏:0      [点我收藏+]

1:添加jar包

2:web.xml中添加验证码过滤器

<!-- 验证码配置 captcha servlet-->
<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>
com.google.code.kaptcha.servlet.KaptchaServlet
</servlet-class>
<init-param>
<description> Border around kaptcha. Legal values are yes or no. </description>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<description>The characters that will create the kaptcha. </description>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>1234567890</param-value>
</init-param>
<init-param>
<description>The number of characters to display. </description>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<description>The size of the font to use. </description>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>50</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>宋体,楷体,微软雅黑</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>

3:页面显示验证码

<td>验证码:</td>
<td><input id="valiCode" name="valiCode" size="10" maxlength="4" class="easyui-textbox" data-options="required:true"/></td>
<td><img id="valiCodeImg" title="看不清楚,换一张" src="/kaptcha.jpg" width="100px" onclick="refreshImg();"/></td>

function refreshImg(){
$("#valiCodeImg").attr("src","${root}/kaptcha.jpg?date"+Math.floor(Math.random()*100));
}

4:后台获取验证码

String kaptchaExpected = getSessionAttr(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); // 获取session中的验证码

 

图片验证码

原文:http://www.cnblogs.com/lulian/p/5456744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!