濮阳杆衣贸易有限公司

主頁 > 知識庫 > 基于ajax實現(xiàn)驗證碼功能

基于ajax實現(xiàn)驗證碼功能

熱門標簽:電銷機器人 長春 上海企業(yè)外呼系統(tǒng)價錢 怎樣在地圖標注文字 大眾點評400電話怎么申請 立陶宛地圖標注 地圖標注推銷坑人 河間市地圖標注app 中國地圖標注不明確情況介紹表 東平縣地圖標注app

本文實例為大家分享了ajax實現(xiàn)驗證碼功能的具體代碼,供大家參考,具體內(nèi)容如下

首先創(chuàng)建一個驗證碼:

%@ page contentType="image/jpeg; charset=utf-8" 
  language="java" import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" 
  pageEncoding="UTF-8"%> 
!-- 以上導(dǎo)入awt和awt.image包 --> 
%! 
  //獲取隨機顏色 
  public Color getColor(){ 
   Random random = new Random(); 
   //使用rgb()隨機產(chǎn)生顏色 
   int r = random.nextInt(256); 
   int g = random.nextInt(256); 
   int b = random.nextInt(256); 
    
   return new Color(r,g,b); 
  } 
   
  //獲取隨機數(shù)字 產(chǎn)生一個4位數(shù) 
  public String getNum(){ 
   String str = ""; 
   Random random = new Random(); 
   for(int i = 0;i  4;i++){ 
    str += random.nextInt(10); //0-9 
   } 
   return str; 
  } 
%> 
 
% 
  /* 清除緩存 */ 
  response.setHeader("pragma", "mo-cache"); 
  response.setHeader("cache-control", "no-cache"); 
  response.setDateHeader("expires", 0); 
  //產(chǎn)生矩形框 
  BufferedImage image = new BufferedImage(80,30,BufferedImage.TYPE_INT_RGB); 
  //獲取畫筆工具 
  Graphics g = image.getGraphics(); 
  //設(shè)置矩形框的顏色 
  g.setColor(new Color(200,200,200)); 
  //設(shè)置坐標和寬高 
  g.fillRect(0, 0, 80, 30); 
     
  //隨機產(chǎn)生干擾線 
  for(int i = 0;i  30;i++){ 
   Random random = new Random(); 
   int x = random.nextInt(80); 
   int y = random.nextInt(30); 
   int x1 = random.nextInt(x + 10); 
   int y1 = random.nextInt(y + 10); 
   //設(shè)置隨機顏色 
   g.setColor(getColor()); 
   //畫出來 
   g.drawLine(x, y, x1, y1); 
  } 
   
  //字的顏色和數(shù)字 
  g.setFont(new Font("Microsoft YaHei",Font.BOLD,16)); 
  g.setColor(Color.BLACK); 
  //獲取隨機數(shù)字 
  String checkNum = getNum(); 
   
  //給字拼接空格 
  StringBuffer sb = new StringBuffer(); 
  for(int i = 0;i  checkNum.length();i++){ 
   sb.append(checkNum.charAt(i) + " "); 
  } 
  //畫出數(shù)字 
  g.drawString(sb.toString(), 15, 20); 
  //存入session域中 
  session.setAttribute("CHECKNUM", checkNum); //例如1010 
  //將圖像以jpeg的形式通過字節(jié)流輸出 
  ImageIO.write(image, "jpeg", response.getOutputStream()); 
  //清除緩存 
  out.clear(); 
  //放入body中 
  out = pageContext.pushBody(); 
%> 

將驗證碼壓縮成圖片,在checkcode.jsp中引用,并在該頁面中利用ajax向服務(wù)器發(fā)送數(shù)據(jù)

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
html> 
 head> 
 base href="%=basePath%>" rel="external nofollow" > 
  
 title>驗證碼/title> 
  
 meta http-equiv="pragma" content="no-cache"> 
 meta http-equiv="cache-control" content="no-cache"> 
 meta http-equiv="expires" content="0">  
 meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 meta http-equiv="description" content="This is my page"> 
 style type="text/css"> 
  table{ 
   margin: 100px auto; 
  } 
   
 /style> 
 /head> 
 
 body> 
  table border="0" align="center"> 
   tr> 
    td>驗證碼/td> 
    td>input type="text" name="checkcode" id="checkcodeID" maxlength="4" size="4">/td> 
    td>img alt="加載失敗" src="image.jsp">/td> 
    td id="show">√√√/td> 
   /tr> 
  /table> 
 /body> 
 script type="text/javascript"> 
  //去除空格 
  function trim(str){ 
   //從左側(cè)開始替換空格 
   str = str.replace(/^\s*/,""); 
   //從左側(cè)開始替換空格 
   str = str.replace(/\s$/,""); 
   return str; 
  } 
  
 /script> 
 
 script type="text/javascript"> 
  //創(chuàng)建ajax對象 
  function createAjax(){ 
   var ajax = null; 
   try{ 
    ajax = new ActiveXObject("microsoft.xmlhttp"); 
   }catch(e){ 
    try{ 
     ajax = new XMLHttpRequest(); 
    }catch(e1){ 
     alert("請更換瀏覽器"); 
    } 
   } 
   return ajax; 
  } 
 /script> 
 
 
 script type="text/javascript"> 
  document.getElementById("checkcodeID").onkeyup = function(){ 
   var checkcode = this.value; 
   //去除空格 
   checkcode = trim(checkcode); 
   if(checkcode.length == 4){ 
    //獲取ajax對象 
    var ajax = createAjax(); 
    //獲取去空格的內(nèi)容 
     
    var method = "POST"; 
    var url = "${pageContext.request.contextPath}/CheckcodeServlet?time="+new Date().getTime(); 
    //準備發(fā)送異步請求 
    ajax.open(method, url); 
    //設(shè)置請求頭POST提交方式才需要 
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded"); 
    //拼接實體內(nèi)容 
    var content = "checkcode=" + checkcode;     
    //發(fā)送請求 
    ajax.send(content); 
     
    //監(jiān)聽服務(wù)器狀態(tài)變化 
    ajax.onreadystatechange = function(){ 
     if(ajax.readyState == 4){ 
      if(ajax.status == 200){ 
       //獲取服務(wù)器內(nèi)容 
       var tip = ajax.responseText; 
       //獲取圖片路徑 然后進行放入td中 
       var img = document.createElement("img"); 
       img.src = tip; 
       img.style.width = "14px"; 
       img.style.height = "14px"; 
       var td = document.getElementById("show"); 
       td.innerHTML = ""; 
       td.appendChild(img); 
      } 
     } 
    } 
     
   } 
    
  } 
 /script> 
/html> 

然后編寫服務(wù)端,接收輸入的信息,判斷是否與驗證碼相互匹配,將對應(yīng)的圖片的路徑以輸出流的方式輸出

public class CheckcodeServlet extends HttpServlet { 
 @Override 
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException { 
  req.setCharacterEncoding("utf-8"); 
  resp.setContentType("text/html;charset=utf-8"); 
  //圖片路徑 
  String tip = "images/MsgError.gif"; 
   
  String checkcode = req.getParameter("checkcode"); 
  //測試 
  System.out.println(checkcode); 
  //獲取session域中的數(shù)字 
  String checkcodeService = (String) req.getSession().getAttribute("CHECKNUM"); 
  //判斷 
  if (checkcode.equals(checkcodeService)) { 
   tip = "images/MsgSent.gif"; 
  } 
  //輸出路徑 
  PrintWriter pw = resp.getWriter(); 
  pw.write(tip); 
  pw.flush(); 
  pw.close(); 
 } 
} 

當(dāng)輸入第4個數(shù)字的時候就會出現(xiàn)提示
運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • asp.net ajax實現(xiàn)無刷新驗證碼
  • 基于Ajax用戶名驗證、服務(wù)條款加載、驗證碼生成的實現(xiàn)方法
  • Ajax實現(xiàn)帶有驗證碼的局部刷新登錄界面
  • Ajax和PHP正則表達式驗證表單及驗證碼
  • PHP+Ajax驗證碼驗證用戶登錄
  • thinkphp驗證碼的實現(xiàn)(form、ajax實現(xiàn)驗證)
  • jsp+ajax實現(xiàn)的局部刷新較驗驗證碼(onblur事件觸發(fā)較驗)
  • PHP生成各種常見驗證碼和Ajax驗證過程
  • Ajax提交表單時驗證碼自動驗證 php后端驗證碼檢測
  • PHP+Ajax實現(xiàn)驗證碼的實時驗證

標簽:玉樹 遼寧 營口 銅川 四川 本溪 益陽 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標題《基于ajax實現(xiàn)驗證碼功能》,本文關(guān)鍵詞  基于,ajax,實現(xiàn),驗證,碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于ajax實現(xiàn)驗證碼功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于ajax實現(xiàn)驗證碼功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    通州市| 唐海县| 仪陇县| 彰化市| 鄂托克旗| 龙泉市| 南靖县| 阿克苏市| 兴文县| 玛多县| 安泽县| 荥阳市| 冕宁县| 平谷区| 平度市| 兴山县| 萨迦县| 元朗区| 鸡西市| 偃师市| 南丰县| 同江市| 黔西| 工布江达县| 贵南县| 盐源县| 濮阳市| 新河县| 南岸区| 禹城市| 永寿县| 阿拉善左旗| 裕民县| 古田县| 永顺县| 新民市| 铜鼓县| 绿春县| 中卫市| 金门县| 如东县|