複製代碼 代碼如下:
makeCertPic.java
package pic;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* @author dzy
* 產生驗證碼圖片
*/
public class makeCertPic {
//驗證碼圖片中可以出現的字元集,可根據需要修改
private char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'};
/**
* 功能:產生彩色驗證碼圖片
* 參數width為產生圖片的寬度,參數height為產生圖片的高度,參數os為頁面的輸出資料流
*/
public String getCertPic(int width, int height, OutputStream os) {
if(width<=0)width=60;
if(height<=0)height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 擷取圖形上下文
Graphics g = image.getGraphics();
// 設定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取隨機產生的認證碼
String strEnsure = "";
// 4代表4位驗證碼,如果要產生更多位的認證碼,則加大數值
for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 將認證碼顯示到映像中,如果要產生更多位的認證碼,增加drawString語句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 隨機產生10個幹擾點
Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 釋放圖形上下文
g.dispose();
try {
// 輸出映像到頁面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
return "";
}
return strEnsure;
}
}
在getCertPic()方法中,首先建立了一個記憶體配置圖像的執行個體對象,再得到此記憶體配置圖像的圖形內容物件,接著再用這個內容物件畫背景、邊框。接下來,隨機產生4個在mapTable[]數組中的字元,組成字串作為驗證字串,並輸出在記憶體中,為了造成一定的幹擾,隨機畫了10個幹擾點,如果要加大幹擾效果,可再多畫一些點。
makeCertPic.jsp頁面用於調用產生驗證碼圖片的JavaBean,並在用戶端顯示,原始碼如下:
makeCertPic.jsp
複製代碼 代碼如下:
<%@page contentType="image/jpeg" %>
<jsp:useBean id="image" scope="page" class="pic.makeCertPic" />
<%
String str=image.getCertPic(0,0,response.getOutputStream());
// 將認證碼存入SESSION
session.setAttribute("certCode", str);
out.clear();
out = pageContext.pushBody();
%>
這裡把產生的驗證碼作為session變數寫入,因此在接收登入頁面輸入的資料頁面中,可用使用者輸入的驗證碼和這個session變數作比較,如果相同則表示驗證通過。
LoginPic.jsp
複製代碼 代碼如下:
<%@ page contentType="text/html;charset=GB2312" %>
<script type="text/javascript">
function reloadcode(){
var verify=document.getElementById('code');
verify.setAttribute('src','makeCertPic.jsp?it='+Math.random());
}
</script>
<html>
<head><title>登入頁面</title></head>
<body>
<table align="center" border="0">
<tralign="center"><td><fontcolor="red"><html:errors/></font></td></tr>
<tr align="center"><td>系統登入</td></tr>
<form. action="loginCheck.jsp" method="post" focus="username">
<tr><td>使用者名稱:<input type="text" name="username"/></td></tr>
<tr><td>密 碼:<input type="password"name="password"/></td></tr>
<tr><td>驗證碼<img src="makeCertPic.jsp" id="code" onclick="reloadcode()" style="cursor: pointer;" alt="看不清楚,換一張"> </td></tr>
<tralign="left"><td>
<input type="submit" value="確定"/></td></tr>
</form>
</table>
</body>
</html>
驗證碼的輸入是否正確可用如下語句驗證:
複製代碼 代碼如下:
String certCode=request.getParameter("certCode");
if(certCode.equals((String)session.getAttribute("certCode")))
out.print("驗證碼輸入正確");
else
out.print("驗證碼輸入錯誤");