服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java验证码组件kaptcha使用方法

java验证码组件kaptcha使用方法

2019-10-21 12:07java之家 JAVA教程

这篇文章主要介绍了java验证码组件kaptcha使用方法,很不错的一个组件,可以在JAVA开发中使用,大家都试试吧

使用方法

项目中导入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();" style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; background: rgb(242, 246, 251); width: 640px; clear: both; border-top: 1px solid rgb(0, 153, 204); border-right: 1px solid rgb(0, 153, 204); border-left: 1px solid rgb(0, 153, 204); border-image: initial; border-bottom: none; font-family: tahoma, arial, "Microsoft YaHei";"> 复制代码代码如下:

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

延伸 · 阅读

精彩推荐