Java EE 產生網站表單的驗證碼,javaee
如今絕大多數的網站或者Web應用程式都實現了驗證碼功能,加入驗證碼可以防止駭客利用惡意程式,在網站中進行頻繁登入,註冊和灌水等操作。
下面的例子主要用到Java EE中Jsp、Servlet技術,希望能為初學者提供協助。
步驟如下:
1、先在MyEclipse下建立一個web project,項目目錄結構如:
2、在src目錄下建立包名為com.servlet的包,然後在包內建立Servlet類,名為:ValidateCodeServlet,該類的作用就是實現產生驗證碼圖片。
代碼及注釋如下:ValidateCodeServlet.java
1 package com.servlet; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 import java.util.Random;10 11 import javax.imageio.ImageIO;12 import javax.servlet.ServletException;13 import javax.servlet.http.HttpServlet;14 import javax.servlet.http.HttpServletRequest;15 import javax.servlet.http.HttpServletResponse;16 17 public class ValidateCodeServlet extends HttpServlet {18 19 20 public void doGet(HttpServletRequest request, HttpServletResponse response)21 throws ServletException, IOException {22 doPost(request,response);23 24 }25 26 27 public void doPost(HttpServletRequest request, HttpServletResponse response)28 throws ServletException, IOException {29 //禁止頁面緩衝30 response.setHeader("Pragma", "No-cache");31 response.setHeader("Cache-Control", "No-cache");32 response.setDateHeader("Expires", 0);33 response.setContentType("image/jpeg"); //設定響應本文的MIME類型為圖片34 35 int width=60,height=20;36 //建立一個位於緩衝中的映像,寬度為60,高度為2037 BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);38 //擷取用於處理圖形內容相關的對象,相當於畫筆39 Graphics g=image.getGraphics();40 Random random=new Random(); //隨機數對象41 g.setColor(getRandomColor(200,250)); //設定映像的背景色42 g.fillRect(0, 0, width, height); //畫一個矩形,座標為(0,0),寬度為60,高度為2043 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); //設定字型格式44 g.setColor(getRandomColor(160,200)); 45 for(int i=0;i<130;i++){ //產生130條幹擾線46 int x =random.nextInt(width);47 int y =random.nextInt(height);48 int x1 =random.nextInt(12); //nextInt(12) 代表0到12的int類型隨機數49 int y1 =random.nextInt(12);50 g.drawLine(x, y, x+x1, y+y1); //在映像的座標(x,y)和座標(x+x1,y+y1)之間畫幹擾線51 }52 String strCode="";53 for(int i=0;i<4;i++){54 String strNumber=String.valueOf(random.nextInt(10));55 strCode=strCode+strNumber;56 //設定字型顏色57 g.setColor(new Color(15+random.nextInt(120),15+random.nextInt(120),15+random.nextInt(120)));58 g.drawString(strNumber, 13*i+6, 16); //將驗證碼依次畫在映像上,座標為(13*i+6,16)59 }60 request.getSession().setAttribute("Code", strCode); //將驗證碼保持到ssesion中61 g.dispose(); //釋放此映像的上下文以及它使用的所以系統資源62 ImageIO.write(image, "JPEG", response.getOutputStream()); //輸出JPEG格式的映像63 response.getOutputStream().flush(); //重新整理輸出資料流64 response.getOutputStream().close(); //關閉輸出資料流65 66 }67 68 //擷取隨機顏色的方法69 public Color getRandomColor(int fc,int bc){70 Random random=new Random();71 Color randomcolor=null;72 if(fc>255)73 fc=255;74 if(bc>255)75 bc=255;76 //設定0-255之間的隨機顏色值77 int r=fc+random.nextInt(bc-fc);78 int g=fc+random.nextInt(bc-fc);79 int b=fc+random.nextInt(bc-fc);80 randomcolor=new Color(r,g,b);81 return randomcolor; //返回具有紅藍綠的不透明的sRGB的顏色82 }83 84 85 86 }ValidateCodeServlet.java
3、建立表單註冊頁面index.jsp,在該頁的表單中使用<img>標籤的src屬性來調用ValidateCodeServlet類產生的圖片驗證碼。
代碼如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head>10 <base href="<%=basePath%>">11 12 <title>驗證碼例子</title>13 <meta http-equiv="pragma" content="no-cache">14 <meta http-equiv="cache-control" content="no-cache">15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">17 <meta http-equiv="description" content="This is my page">18 </head>19 20 <body>21 <form action="" method="post">22 <table align="center">23 <tr>24 <td>使用者名稱:</td>25 <td><input type="text" name="name"></td>26 </tr>27 <tr>28 <td>密碼:</td>29 <td><input type="password" name="name"></td>30 </tr>31 <tr>32 <td>驗證碼:</td>33 <td><img alt="" src="ValidateCodeServlet" /></td>34 </tr>35 <tr>36 <td>請輸入驗證碼:</td>37 <td><input type="text" name="code"></td>38 </tr>39 </table>40 </form>41 </body>42 </html>index.jsp
運行效果如下: