java验证码组件kaptcha使用方法

作者:kgmucom   在线用户:22
风之恋奇迹私服技术网是一家提供最新互联网编程技术网站

kaptcha是生成的非常实用的工具,使用它,您可以生成各种身份验证代码,因为它是可配置的,那么java验证码组件kaptcha使用方法大家都清楚吗?别着急,下面就让爱站技术频道小编带你来学习吧!

使用方法

项目中导入kaptcha-2.3.jar包

在web.xml里面新增: 

 

<!-- 登陆验证码Kaptcha -->
    <servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
                   <init-param>
            <param-name>kaptcha.producer.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </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.textproducer.char.space</param-name>
                            <param-value>10</param-value>
                   </init-param>
                   <init-param>
                            <param-name>kaptcha.border</param-name>
                            <param-value>no</param-value>
                   </init-param>
                   <init-param>
                            <param-name>kaptcha.border.thickness</param-name>
                            <param-value>0</param-value>
                   </init-param>
                   <init-param>
                            <param-name>kaptcha.textproducer.font.size</param-name>
                            <param-value>35</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.image.width</param-name>
            <param-value>150</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>50</param-value>
        </init-param>
        <init-param>
                            <param-name>kaptcha.background.clear.from</param-name>
                            <param-value>255,255,255</param-value>
                   </init-param>
                   <init-param>
                            <param-name>kaptcha.background.clear.to</param-name>
                            <param-value>255,255,255</param-value>
                   </init-param>
                   <init-param>
            <param-name>kaptcha.textproducer.char.string</param-name>
            <param-value>0123456789</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.names</param-name>
            <param-value>Arial Black</param-value>
        </init-param>
         <init-param>
            <param-name>kaptcha.noise.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.NoNoise</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Kaptcha</servlet-name>
        <url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>

 

在页面上新增

 

 

<img id="img" src="kaptcha.jpg" width="90" height="25" onclick="refresh();" />
function refresh() {
                  document.getElementById('img').src="kaptcha.jpg?"+Math.random();
}

 

在action里获取验证码ID:

 

 

request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

看了上文介绍,大家对于java验证码组件kaptcha使用方法大概了解了吗?更多的编程资讯尽在爱站技术频道,很高兴能够帮助到大家!

提供最全面的奇迹sf开服版本和最新的奇迹私服开服技术信息,奇迹私服文库让每一位奇迹私服玩家找到自己需要的版本技术文章

上一篇:如何整合SpringMVC和Swagger

下一篇:详解JAVA IO API使用方法

请发表您的评论