在開發中驗證碼是比較經常使用到有效防止這種問題對某一個特定註冊用戶用特定程序暴力破解方式進行不斷的登錄嘗試的方式。
此演示程序包括三個文件:
1.login.jsp:登陸頁面
2.code.jsp:生成驗證碼圖片頁面
3.check.jsp:驗證結果
code.jsp
<%@ page contentType="p_w_picpath/jpeg" import="java.awt.*,
java.awt.p_w_picpath.*,java.util.*,javax.p_w_picpathio.*" %>
<%
// 在內存中建立圖象
int width=60, height=20;
BufferedImage p_w_picpath = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = p_w_picpath.getGraphics();
// 設定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 隨機產生的認證碼(4位數字)
String rand =""+ (Math.random()*10000);
rand = rand.substring(0,rand.indexOf("."));
switch(rand.length())
{
case 1: rand = "000"+rand; break;
case 2: rand = "00"+rand; break;
case 3: rand = "0"+rand; break;
default: rand = rand.substring(0,4); break;
}
// 將認證碼存入SESSION
session.setAttribute("rand",rand);
// 將認證碼顯示到圖象中
g.setColor(Color.black);
Integer tempNumber = new Integer(rand);
String numberStr = tempNumber.toString();
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String Str = numberStr.substring(0,1);
g.drawString(Str,8,17);www.fkJAVA.org
Str = numberStr.substring(1,2);
g.drawString(Str,20,15);
Str = numberStr.substring(2,3);
g.drawString(Str,35,18);
Str = numberStr.substring(3,4);
g.drawString(Str,45,15);
// 隨機產生88個干擾點,使圖象中的認證碼不易被其它程序探測到
Random random = new Random();
for (int i=0;i<20;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x,y,0,0);
}html
// 圖象生效
g.dispose();
// 輸出圖象到頁面
ImageIO.write(p_w_picpath, "JPEG", response.getOutputStream());
//在頁面上調用 <img src="/yourPath/checkNum.jsp" />
%>
login.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>認證碼輸入頁面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<form method=post action="check.jsp">
<table>
<tr>
<td align=left>系統產生的認證碼:</td>
<td><img border=0 src="code.jsp"></td>
</tr>
<tr>
<td align=left>輸入上面的認證碼:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交檢測"></td>
</tr>
</form>
</body>
</html>
check.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>認證碼驗證頁面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<%
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("rand");
%>
系統產生的認證碼爲: <%= rand %><br>
您輸入的認證碼爲: <%= input %><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>輸入相同,認證成功!</font>
<%
} else {
%>
<font color=red>輸入不一樣,認證失敗!</font>
<%
}
%>
</body>
</html>
以上代碼能夠實現,我驗證過了,都可正常使用。更多更全的IT資訊,盡在博洋教育。您有任何關於高級java培訓的疑問,歡迎諮詢在線老師。
java