簡易的網頁驗證碼

來源:互聯網
上載者:User

標籤:imp   logs   lan   als   數值   inpu   plain   技術分享   title   

通過使用Java圖形介面設計(AWT)來繪製一個簡易的驗證碼,效果如下:

 

整體的效果很low,較為現代的網頁介面估計不會使用,但作為後台管理介面所需還可以的,在這裡還是進行個記錄,以備不時之需

第一步:構建驗證碼
<%@ page language="java" import="java.util.*,java.awt.*" pageEncoding="UTF-8"%><%@ page import="javax.imageio.*"%><%@ page import="java.awt.image.*" %><!-- 驗證碼 --><html>  <head>    <title>網上書店</title>  </head>    <body>    <%    response.setHeader("Cache-Control","no-cache");    //記憶體中建立映像    int width=60,height=20;    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);    //擷取畫筆    Graphics g=image.getGraphics();    //設定背景色    g.setColor(new Color(200,200,200));    g.fillRect(0,0,width,height);    //隨機數    Random rnd=new Random();    int randNum=rnd.nextInt(8999)+1000;    String randStr=String.valueOf(randNum);    //將驗證碼存入session    session.setAttribute("randStr",randStr);    //將驗證碼顯示在映像    g.setColor(Color.black);    g.setFont(new Font("",Font.PLAIN,20));    g.drawString(randStr,10,17);    //產生100個幹擾點(就是圖片上的噪點)    for(int i=0;i<100;i++){      int x=rnd.nextInt(width);      int y=rnd.nextInt(height);      g.drawOval(x,y,1,1);    }    //輸出映像    ImageIO.write(image,"JPEG",response.getOutputStream());    out.clear();    out=pageContext.pushBody();     %>  </body></html>

這個JSP頁面,名叫code.jsp,其中嵌入的Java代碼就是驗證碼的實現方法。值得注意的有:

int width=60,height=20;BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

其中width,height分別設定外部範圍的寬和高,也就是灰色地區的寬和高,BufferedImage.TYPE_INT_RGB設定這個地區是由RGB三色構成,值為int型

   //設定背景色    g.setColor(new Color(200,200,200));

在這裡我指定了RGB的顏色,所以呈現一個灰色地區

int randNum=rnd.nextInt(8999)+1000;

使用隨機數,這裡隨機數的範圍是1000~9999來輸出一個4位的隨機數

String randStr=String.valueOf(randNum);//將驗證碼存入sessionsession.setAttribute("randStr",randStr);

這裡將具體數值存入session,名字叫randStr,之後會使用

第二步:在需要的頁面引入這個JSP

 

<td>
<input type="text" name="code" size="10"> <img id="imgValidate" src="code.jsp" onclick="refresh()" title="看不清可單擊圖片重新整理">
</td>

 

增加一個輸入項,name="code",Action中要使用,code.jsp就是我們在上面寫的驗證碼頁面,為這個img標籤來個id="imgValidate",JavaScript中要使用,同時增加一個JavaScript方法用於點擊驗證碼進行重新整理

JavaScript代碼:

  <script type="text/javascript">    function refresh(){     document.getElementById("imgValidate").src="code.jsp?"+new Date();    } </script>

根據目前時間進行重新整理,保證兩次驗證碼不一樣

第三步:在Action中獲得使用者輸入的驗證碼與實際顯示的驗證碼進行比較

在Action中增加一個名為code的String變數,並產生其set/get方法

private String code;public String getCode() {    return code;}public void setCode(String code) {    this.code = code;}

在需要驗證的方法裡進行比較:

String randStr=(String) session.getAttribute("randStr");    if(code!=null){     if(code.equals(randStr)){         System.out.println("驗證碼正確");      }else{         System.out.println("驗證碼錯誤");      }}

這裡使用到了之前傳入session中的名為randStr的值,使其與使用者輸入的code變數進行比較

 

簡易的網頁驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.