您的足迹:首页 > JAVA >java验证码插件-kaptcha

java验证码插件-kaptcha

kaptcha一个很好用的验证码插件,java版的,很不错的一个插件,只用简单的在web.xml中设置几个属性,一个很漂亮的验证码就出来了。其他的参数都可以自己设置,最牛的就是提供了接口,可以自己定义哦。
html代码
<form action="submit.action">
    <img src="kaptcha.jpg" /> <input type="text" name="kaptcha" value="" />
</form>
web.xml代码
<servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>Kaptcha</servlet-name>
        <url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
 
java代码
String kaptchaExpected = (String)request.getSession()
    .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String kaptchaReceived = request.getParameter("kaptcha");
 
if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected))
{
    setError("kaptcha", "Invalid validation code.");
}
 
js代码
<img src="/kaptcha" width="200" id="kaptchaImage" />


<script type="text/javascript">
    $(function(){
        $('#kaptchaImage').click(function () { 
            $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); 
        })
    });
</script>
<br /><small>Can't read the image? Click it to get a new one.</small>

 
 
图片
04122201_gxzV.gif
 
更详细配置
<init-param>   
             <param-name>kaptcha.border</param-name>   
             <param-value>yes</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.border.color</param-name>   
             <param-value>105,179,90</param-value>   
         </init-param>  
         <init-param>   
             <param-name>kaptcha.textproducer.font.color</param-name>   
             <param-value>black</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.image.width</param-name>   
             <param-value>500</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.image.height</param-name>   
             <param-value>300</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.textproducer.font.size</param-name>   
             <param-value>90</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.session.key</param-name>   
             <param-value>code</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.textproducer.char.length</param-name>   
             <param-value>4</param-value>   
         </init-param>   
         <init-param>   
             <param-name>kaptcha.textproducer.font.names</param-name>   
             <param-value>宋体,楷体,微软雅黑</param-value>   
         </init-param> 
 
 
 
 
 
本博客所有文章如无特别注明均为原创。作者:小天复制或转载请以超链接形式注明转自 钟天才的博客
原文地址《java验证码插件-kaptcha

相关推荐

网友评论(0)