码迷,mamicode.com
首页 > 其他好文 > 详细

图片验证码

时间:2016-05-03 23:44:47      阅读:222      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!