java下應用kaptcha生成驗證碼。本站提示廣大學習愛好者:(java下應用kaptcha生成驗證碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java下應用kaptcha生成驗證碼正文
kaptcha是一個簡略好用的驗證碼生成對象,經由過程設置裝備擺設,可以本身界說驗證碼年夜小、色彩、顯示的字符等等。上面就來說一下若何應用kaptcha生成驗證碼和在辦事器端掏出驗證碼停止校驗。
1、搭建測試情況
1.1、創立Web測試項目
新建一個Web項目,並將kaptcha-2.3.2.jar放在項目標WEB-INF/lib目次下,以下圖所示:
1.2、在web.xml文件設置裝備擺設生成驗證碼的KaptchaServlet
KaptchaServlet的具體設置裝備擺設以下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- kaptcha驗證碼設置裝備擺設 --> <servlet> <!-- 生成圖片的Servlet --> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.谷歌.code.kaptcha.servlet.KaptchaServlet</servlet-class> <!-- 能否有邊框--> <init-param> <param-name>kaptcha.border</param-name> <param-value>no</param-value> </init-param> <!-- 字體色彩 --> <init-param> <param-name>kaptcha.textproducer.font.color</param-name> <param-value>red</param-value> </init-param> <!-- 圖片寬度 --> <init-param> <param-name>kaptcha.image.width</param-name> <param-value>135</param-value> </init-param> <!-- 應用哪些字符生成驗證碼 --> <init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>ACDEFHKPRSTWX345679</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.textproducer.font.size</param-name> <param-value>43</param-value> </init-param> <!-- 攪擾線的色彩 --> <init-param> <param-name>kaptcha.noise.color</param-name> <param-value>black</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.names</param-name> <param-value>Arial</param-value> </init-param> </servlet> <!-- 映照的url --> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/Kaptcha.jpg</url-pattern> </servlet-mapping> </web-app>
1.3、顯示生成的驗證碼
在頁面index.jsp顯示驗證碼
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>顯示KaptchaServlet生成的驗證碼</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script> <script type="text/javascript"> //點擊切換驗證碼 function changeVerifyCode(){ $("#yzmImg").attr("src","Kaptcha.jpg?"+Math.floor(Math.random()*100)); } //提交 function doSubmit() { var verifyCodeValue = $("#verifyCode").val(); if(verifyCodeValue.replace(/\s/g,"") == "") { alert("請輸出驗證碼"); }else { //提交前先異步檢討驗證碼能否輸出准確 var verifyUrl = "${pageContext.request.contextPath}/servlet/VerifyServlet?verifyCode="+verifyCodeValue; $.ajax({ type:"GET", url:verifyUrl, success:function(returnData){ if(returnData!="Y") { alert("請輸出准確的驗證碼!"); }else { //驗證碼准確,停止提交操作 alert("驗證碼輸出准確,提交表單"); } }, error:function(e){ alert(e); } }); } } </script> </head> <body> <form> <table> <tr> <td> 請輸出驗證碼: </td> <td> <input type="text" name="verifyCode" id="verifyCode"> <img src="Kaptcha.jpg" onclick="changeVerifyCode()" id="yzmImg" > <a href="javascript:void(0)" onclick="changeVerifyCode()">看不清,換一張</a> </td> </tr> <tr> <td> </td> <td> <input type="button" value="提交" onclick="doSubmit()"> </td> </tr> </table> </form> </body> </html>
運轉後果以下圖所示:
1.4、在辦事器端驗證提交的驗證碼
用戶在form表單中提交了驗證碼以後,我們會在辦事器端停止驗證,編寫一個VerifyServlet,代碼以下:
/** * */ package me.gacl.web.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class VerifyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charaset=utf-8"); response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); PrintWriter out = null; try { //呼應數據 String resultData; //獲得傳過去的驗證碼 String verifyCode = request.getParameter("verifyCode"); System.out.println("verifyCode----"+verifyCode); if(verifyCode=="") { resultData = "N"; }else { //獲得kaptcha生成寄存在session中的驗證碼 String kaptchaValue = (String) request.getSession().getAttribute(com.谷歌.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); //比擬輸出的驗證碼和現實生成的驗證碼能否雷同 if(kaptchaValue == null || kaptchaValue == ""||!verifyCode.equalsIgnoreCase(kaptchaValue)) { resultData = "N"; }else { resultData = "Y"; } } out = response.getWriter(); out.write(resultData); out.flush(); }catch(Exception e) { e.printStackTrace(); }finally { if(out != null) { out.close(); } } } }
在web.xml中注冊VerifyServlet
<!-- 檢討驗證碼能否輸出准確 --> <servlet> <servlet-name>VerifyServlet</servlet-name> <servlet-class>me.gacl.web.controller.VerifyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>VerifyServlet</servlet-name> <url-pattern>/servlet/VerifyServlet</url-pattern> </servlet-mapping>
運轉成果以下:
1、驗證碼不輸出
2、輸出毛病的驗證碼
3、輸出准確的驗證碼
應用kaptcha生成驗證碼感到挺不錯的,異常好用和便利,願望年夜家可以闇練控制。