标签:
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