kaptcha 驗證碼使用

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

web.xml配置
js方法:

$(function(){  //生成驗證碼         
    $('#kaptchaImage').click(function () {  
    $(this).hide().attr('src', '<%=rootPath%>Kaptcha.jpg?' + Math.floor(Math.random()*100) ).fadeIn(); });      
});  



function changeCode() {  //刷新
    $('#kaptchaImage').hide().attr('src', '<%=rootPath%>Kaptcha.jpg?' + Math.floor(Math.random()*100) ).fadeIn();  
    event.cancelBubble=true;  
}

頁面input

 <tr>
    <td ><dt>驗證碼</dt></td>
    <td><input name="kaptcha" type="text" id="kaptcha" maxlength="10" class="easyui-validatebox"  style="height: 28px;margin-top: 13px;width: 100%;"/></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td> &nbsp; </td>
    <td><img src="<%=rootPath%>Kaptcha.jpg" id="kaptchaImage"  style="margin-bottom: -3px;width: 71%;height: 49px;"/></td>
    <td align="left">  <a href="#" onclick="changeCode()">看不清?換一張</a>  </td>
  </tr>

後臺接收的方法

HttpServletRequest request =ServletActionContext.getRequest();
    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)) {
                正確
            } else {

                      錯誤
            }

jar包下載地址
我用的是kaptcha-2.3.2.jar,可以百度搜索下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章