生成有
產生驗證碼圖片的文件
<%@ page contentType="image/jpeg" import="java
<%!
Color getRandColor(int fc
Random random = new Random();
if(fc>
if(bc>
int r=fc+random
int g=fc+random
int b=fc+random
return new Color(r
}
%>
<%
//設置頁面不緩存
response
response
response
// 在內存中創建圖象
int width=
BufferedImage image = new BufferedImage(width
// 獲取圖形上下文
Graphics g = image
//生成隨機類
Random random = new Random();
// 設定背景色
g
g
//設定字體
g
//畫邊框
//g
//g
// 隨機產生
g
for (int i=
{
int x = random
int y = random
int xl = random
int yl = random
g
}
// 取隨機產生的認證碼(
String sRand="";
for (int i=
String rand=String
sRand+=rand;
// 將認證碼顯示到圖象中
g
g
}
// 將認證碼存入session
session
// 圖象生效
g
// 輸出圖象到頁面
ImageIO
%>
<%@ page contentType="text/html;charset=gb
<!DOCTYPE HTML PUBLIC "
<html>
<head>
<title>認證碼輸入頁面</title>
<meta http
<META HTTP
<META HTTP
<META HTTP
</head>
<body>
<form method=post action="check
<table>
<tr>
<td align=left>系統產生的認證碼
<td><img border=
</tr>
<tr>
<td align=left>輸入上面的認證碼
<td><input type=text name=rand maxlength=
</tr>
<tr>
<td colspan=
</tr>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=gb
<html>
<head>
<title>認證碼驗證頁面</title>
<meta http
<META HTTP
<META HTTP
<META HTTP
</head>
<body>
<%
String rand = (String)session
String input = request
%>
系統產生的認證碼為
您輸入的認證碼為
<br>
<%
if (rand
%>
<font color=green>輸入相同
<%
} else {
%>
<font color=red>輸入不同
<%
}
%>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20610.html