校验码组件kaptcha

1、到http://code.google.com/p/kaptcha/ 下载jar文件到 WEB-INF/lib目录下

2、在web.xml中配置servlet:

<servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
        <init-param>
            <param-name>kaptcha.image.width</param-name>
            <param-value>65</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>30</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.size</param-name>
            <param-value>25</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.obscurificator.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
        </init-param>
          </servlet>

       <servlet-mapping>
           <servlet-name>Kaptcha</servlet-name>
           <url-pattern>/kaptcha.jpg</url-pattern>
       </servlet-mapping>


        如果需要全部数字
        <init-param>   
            <param-name>kaptcha.textproducer.char.string</param-name>   
            <param-value>0123456789</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>

3.html中添加代码:

<img src="kaptcha.jpg" />

4.Java中获取真实的验证码:

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


你可能感兴趣的:(kaptcha)