google的验证码

先下载  kaptcha-2.3.2.jar 包,maven获取代码:

<!-- 验证码 -->

        <dependency>

            <groupId>com.google.code</groupId>

            <artifactId>kaptcha</artifactId>

            <version>2.3.2</version>

        </dependency>

 

web.xml增加如下代码

<!-- google的验证码插件 -->

  <servlet> 

        <servlet-name>Kaptcha</servlet-name> 

        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>

        <!-- 全都要数字 -->

        <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>

        <init-param>

            <param-name>kaptcha.border</param-name>

            <param-value>no</param-value>

        </init-param>

        <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>

前台jsp显示:

<dd>验证码:</dd>

                    <dd>

                        <input type="text" id="yzm" name="yzm" class="inputyzm" maxlength="4"/>

                        <img style="padding-left: 8px; margin-bottom:8px; height:25px;" id="yzmimg" src="<%=basePath%>kaptcha.jpg" />

                    </dd>

后台controller验证

//检查验证码

               String yzm=request.getParameter("yzm");  

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

           

               if(!yzm.equals(kaptchaExpected))

            {

                   responseTxt("2");

                   return;

            }

 

你可能感兴趣的:(Google)