欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年11月08日 11:44:13   作者:evan_qb  
這篇文章主要為大家詳細(xì)介紹了基于ajax實(shí)現(xiàn)驗(yàn)證碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

首先創(chuàng)建一個(gè)驗(yàn)證碼:

<%@ 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包 --> 
<%! 
  //獲取隨機(jī)顏色 
  public Color getColor(){ 
   Random random = new Random(); 
   //使用rgb()隨機(jī)產(chǎn)生顏色 
   int r = random.nextInt(256); 
   int g = random.nextInt(256); 
   int b = random.nextInt(256); 
    
   return new Color(r,g,b); 
  } 
   
  //獲取隨機(jī)數(shù)字 產(chǎn)生一個(gè)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è)置坐標(biāo)和寬高 
  g.fillRect(0, 0, 80, 30); 
     
  //隨機(jī)產(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è)置隨機(jī)顏色 
   g.setColor(getColor()); 
   //畫出來 
   g.drawLine(x, y, x1, y1); 
  } 
   
  //字的顏色和數(shù)字 
  g.setFont(new Font("Microsoft YaHei",Font.BOLD,16)); 
  g.setColor(Color.BLACK); 
  //獲取隨機(jī)數(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(); 
%> 

將驗(yàn)證碼壓縮成圖片,在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>驗(yàn)證碼</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>驗(yàn)證碼</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對(duì)象 
  function createAjax(){ 
   var ajax = null; 
   try{ 
    ajax = new ActiveXObject("microsoft.xmlhttp"); 
   }catch(e){ 
    try{ 
     ajax = new XMLHttpRequest(); 
    }catch(e1){ 
     alert("請(qǐng)更換瀏覽器"); 
    } 
   } 
   return ajax; 
  } 
 </script> 
 
 
 <script type="text/javascript"> 
  document.getElementById("checkcodeID").onkeyup = function(){ 
   var checkcode = this.value; 
   //去除空格 
   checkcode = trim(checkcode); 
   if(checkcode.length == 4){ 
    //獲取ajax對(duì)象 
    var ajax = createAjax(); 
    //獲取去空格的內(nèi)容 
     
    var method = "POST"; 
    var url = "${pageContext.request.contextPath}/CheckcodeServlet?time="+new Date().getTime(); 
    //準(zhǔn)備發(fā)送異步請(qǐng)求 
    ajax.open(method, url); 
    //設(shè)置請(qǐng)求頭POST提交方式才需要 
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded"); 
    //拼接實(shí)體內(nèi)容 
    var content = "checkcode=" + checkcode;     
    //發(fā)送請(qǐng)求 
    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; 
       //獲取圖片路徑 然后進(jìn)行放入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àn)證碼相互匹配,將對(duì)應(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個(gè)數(shù)字的時(shí)候就會(huì)出現(xiàn)提示
運(yùn)行結(jié)果:

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

相關(guān)文章

  • 使用ajax操作 JavaScript 對(duì)象

    使用ajax操作 JavaScript 對(duì)象

    這篇文章主要介紹了使用ajax操作 JavaScript 對(duì)象方法的相關(guān)資料,需要的朋友可以參考下
    2015-01-01
  • AJAX在Post中文的時(shí)候亂碼的解決方法

    AJAX在Post中文的時(shí)候亂碼的解決方法

    AJAX在Post中文的時(shí)候亂碼的解決方法...
    2007-03-03
  • AJAX 異步傳輸數(shù)據(jù)的問題

    AJAX 異步傳輸數(shù)據(jù)的問題

    暫時(shí)把script中的‘+’都用‘-’代替,index += 1;改成index -= -1;呵呵,以后有人看到這段自動(dòng)生成的詭異腳本,不知道會(huì)作何感想,但現(xiàn)在也只能如此。
    2008-12-12
  • Ajax的特性及亂碼問題

    Ajax的特性及亂碼問題

    ajax的全稱是asynchronous javascript and XML ,它是異步的js和XML。它是局部刷新,異步操作。這篇文章給大家介紹了ajax的特性及亂碼問題,感興趣的朋友一起看看吧
    2017-07-07
  • 通過Ajax兩種方式講解Struts2接收數(shù)組表單的方法

    通過Ajax兩種方式講解Struts2接收數(shù)組表單的方法

    使用struts2表單傳值,可以傳一個(gè)或者是作為一個(gè)對(duì)象的各個(gè)屬性傳,都非常靈活便捷。但是如果我們需要傳一個(gè)數(shù)組并希望struts正確接收,該怎么處理呢?接下來,通過本文給大家介紹通過Ajax兩種方式講解Struts2接收數(shù)組表單的方法,需要的朋友可以參考下
    2015-10-10
  • IE6中ajax aborted錯(cuò)誤請(qǐng)求中斷解決方法

    IE6中ajax aborted錯(cuò)誤請(qǐng)求中斷解決方法

    給a標(biāo)簽綁定了一個(gè)click事件用來觸發(fā)ajax請(qǐng)求,在IE6中,請(qǐng)求時(shí)常會(huì)被中斷,在其他瀏覽器中都一切正常,具體解決方法如下,感興趣的朋友可以參考下
    2013-06-06
  • 簡述Ajax的優(yōu)點(diǎn)與缺點(diǎn)

    簡述Ajax的優(yōu)點(diǎn)與缺點(diǎn)

    這篇文章主要介紹了Ajax的優(yōu)點(diǎn)與缺點(diǎn)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • 關(guān)于ajax對(duì)象一些常用屬性、事件和方法大小寫比較常見的問題總結(jié)

    關(guān)于ajax對(duì)象一些常用屬性、事件和方法大小寫比較常見的問題總結(jié)

    最近比較空閑,于是抽個(gè)時(shí)間整理些關(guān)于ajax方法的東東。在項(xiàng)目中經(jīng)常發(fā)現(xiàn)ajax板塊好多問題都是屬性,方法,事件大小寫不區(qū)分問題,最終導(dǎo)致了程序運(yùn)行出現(xiàn)麻煩,下面給大家介紹關(guān)于ajax對(duì)象一些常用屬性、事件和方法大小寫比較常見的問題總結(jié)
    2015-10-10
  • Ajax請(qǐng)求內(nèi)嵌套Ajax請(qǐng)求示例代碼

    Ajax請(qǐng)求內(nèi)嵌套Ajax請(qǐng)求示例代碼

    把全國省市的兩個(gè)XML文件整合成一個(gè)JSON格式的數(shù)據(jù),就想到了用Ajax嵌套的方法來解決,查找資料,加個(gè)async:false這個(gè)Ajax參數(shù)就行了
    2014-08-08
  • 簡單介紹不用庫(框架)自己寫ajax

    簡單介紹不用庫(框架)自己寫ajax

    本篇文章跟大家介紹不用庫(框架)自己寫ajax,感興趣的朋友跟著腳本之家小編一起學(xué)習(xí)吧
    2015-10-10

最新評(píng)論