jsp 生成驗(yàn)證碼代碼
更新時(shí)間:2009年06月04日 02:26:44 作者:
本頁(yè)面 image.jsp 功能:實(shí)現(xiàn)自動(dòng)生成4位的驗(yàn)證碼(有數(shù)字和字母構(gòu)成
調(diào)用方法:在jsp頁(yè)面用圖像標(biāo)簽便可以直接調(diào)用如下是標(biāo)簽代碼
<img border=0 src="image.jsp">,只需要把該代碼發(fā)在驗(yàn)證碼要顯示的區(qū)域就可以了)
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
public static String code="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Color getRandColor(int fc,int bc){//給定范圍獲得隨機(jī)顏色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
//設(shè)置頁(yè)面不緩存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 在內(nèi)存中創(chuàng)建圖象,設(shè)置圖片的顯示大小
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
//生成隨機(jī)類
Random random = new Random();
// 設(shè)定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
//設(shè)定字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//畫邊框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// 取隨機(jī)產(chǎn)生的認(rèn)證碼(由數(shù)字和字母組長(zhǎng)的)
String sRand="";
for (int i=0;i<4;i++){
int rand=random.nextInt(62);
sRand+=String.valueOf(code.charAt(rand));
// 將認(rèn)證碼顯示到圖象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));// 調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
g.drawString(String.valueOf(code.charAt(rand)),13*i+6,16);
}
// 將認(rèn)證碼存入SESSION
session.setAttribute("rand",sRand);
// 圖象生效
g.dispose();
// 輸出圖象到頁(yè)面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
.下面是一個(gè)test.jsp,來(lái)進(jìn)行驗(yàn)證碼生成的測(cè)試,然后交給check.jsp來(lái)處理
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>認(rèn)證碼輸入頁(yè)面</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>系統(tǒng)產(chǎn)生的認(rèn)證碼:</td>
<td><img border=0 src="image.jsp"></td>
</tr>
<tr>
<td align=left>輸入上面的認(rèn)證碼:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交檢測(cè)"></td>
</tr>
</form>
</body>
</html>
.下面是一個(gè)check.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>認(rèn)證碼驗(yàn)證頁(yè)面</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");
%>
系統(tǒng)產(chǎn)生的認(rèn)證碼為: <%= rand %><br>
您輸入的認(rèn)證碼為: <%= input %><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>輸入相同,認(rèn)證成功!</font>
<%
} else {
%>
<font color=red>輸入不同,認(rèn)證失敗!</font>
<%
}
%>
</body>
</html>
<img border=0 src="image.jsp">,只需要把該代碼發(fā)在驗(yàn)證碼要顯示的區(qū)域就可以了)
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
public static String code="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Color getRandColor(int fc,int bc){//給定范圍獲得隨機(jī)顏色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
//設(shè)置頁(yè)面不緩存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 在內(nèi)存中創(chuàng)建圖象,設(shè)置圖片的顯示大小
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
//生成隨機(jī)類
Random random = new Random();
// 設(shè)定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
//設(shè)定字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//畫邊框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// 取隨機(jī)產(chǎn)生的認(rèn)證碼(由數(shù)字和字母組長(zhǎng)的)
String sRand="";
for (int i=0;i<4;i++){
int rand=random.nextInt(62);
sRand+=String.valueOf(code.charAt(rand));
// 將認(rèn)證碼顯示到圖象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));// 調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
g.drawString(String.valueOf(code.charAt(rand)),13*i+6,16);
}
// 將認(rèn)證碼存入SESSION
session.setAttribute("rand",sRand);
// 圖象生效
g.dispose();
// 輸出圖象到頁(yè)面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
.下面是一個(gè)test.jsp,來(lái)進(jìn)行驗(yàn)證碼生成的測(cè)試,然后交給check.jsp來(lái)處理
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>認(rèn)證碼輸入頁(yè)面</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>系統(tǒng)產(chǎn)生的認(rèn)證碼:</td>
<td><img border=0 src="image.jsp"></td>
</tr>
<tr>
<td align=left>輸入上面的認(rèn)證碼:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交檢測(cè)"></td>
</tr>
</form>
</body>
</html>
.下面是一個(gè)check.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>認(rèn)證碼驗(yàn)證頁(yè)面</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");
%>
系統(tǒng)產(chǎn)生的認(rèn)證碼為: <%= rand %><br>
您輸入的認(rèn)證碼為: <%= input %><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>輸入相同,認(rèn)證成功!</font>
<%
} else {
%>
<font color=red>輸入不同,認(rèn)證失敗!</font>
<%
}
%>
</body>
</html>
您可能感興趣的文章:
- 使用canvas及js簡(jiǎn)單生成驗(yàn)證碼方法
- JavaScript生成驗(yàn)證碼并實(shí)現(xiàn)驗(yàn)證功能
- js生成驗(yàn)證碼并直接在前端判斷
- JSP動(dòng)態(tài)生成驗(yàn)證碼存儲(chǔ)在session作用范圍內(nèi)
- JavaScript中用于生成隨機(jī)數(shù)的Math.random()方法
- javascript中Math.random()使用詳解
- 使用js Math.random()函數(shù)生成n到m間的隨機(jī)數(shù)字
- javascript Math.random()隨機(jī)數(shù)函數(shù)
- JavaScript使用Math.random()生成簡(jiǎn)單的驗(yàn)證碼
相關(guān)文章
使用MongoDB和JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的購(gòu)物車系統(tǒng)實(shí)例
本篇文章主要介紹了使用MongoDB和JSP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的購(gòu)物車系統(tǒng)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄
這篇文章主要為大家詳細(xì)介紹了jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
讀取數(shù)據(jù)庫(kù)的數(shù)據(jù)并整合成3D餅圖在jsp中顯示詳解
這篇文章主要給大家介紹了關(guān)于讀取數(shù)據(jù)庫(kù)的數(shù)據(jù)并整合成3D餅圖在jsp中顯示的相關(guān)資料,文中通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
一個(gè)開發(fā)人員眼中的JSP技術(shù)(上)
一個(gè)開發(fā)人員眼中的JSP技術(shù)(上)...2006-10-10
JSP實(shí)現(xiàn)計(jì)算器功能(網(wǎng)頁(yè)版)
這篇文章講述了JSP實(shí)現(xiàn)計(jì)算器功能的詳細(xì)代碼,網(wǎng)頁(yè)版的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12

