hujuan's blog
BlogJava
首頁
新隨筆
聯系
聚合
管理
隨筆分類
JAVA(12)
(rss)
JavaScript(2)
(rss)
jQuery(2)
(rss)
JSP(1)
(rss)
Live(1)
(rss)
Oracle(6)
(rss)
Other Database
(rss)
文章分類
J2SE(1)
(rss)
隨筆檔案
2009年7月 (4)
2009年6月 (7)
2009年1月 (2)
2008年10月 (2)
2008年3月 (3)
2007年7月 (2)
2007年6月 (2)
2007年5月 (6)
文章檔案
2007年5月 (1)
最新隨筆
1.?bean的作用域
2.?spring2.5實例化bean的三種方式
3.?dom4j創建解析xml
4.?jdom創建解析xml
5.?JPA+Hibernate 3.3 學習小結——一對一雙向關聯映射
6.?JPA+Hibernate 3.3 學習小結——多對多關系關系映射
7.?JPA+Hibernate 3.3 學習小結——雙向多對一關聯及級聯操作
8.?JPA+Hibernate 3.3 學習小結——使用JQL語句
9.?JPA+Hibernate 3.3 學習小結——增刪改查
10.?JPA+Hibernate 3.3 學習小結——基本屬性映射
11.?JPA+Hibernate 3.3 學習小結——第一個JPA程序
12.?jQuery基礎偽鏈接
13.?jQuery基礎ready
14.?CSS學習筆記(二)
15.?CSS學習筆記(一)
最新評論
1.?re: 添加動態驗證碼
評論內容較長,點擊標題查看
--mable
2.?re: JPA+Hibernate 3.3 學習小結——基本屬性映射[未登錄]
非常感謝,你的學習材料,我也要好好學下JPA和HIBERNATE才行啊
--ssh
3.?re: JDBC 工作原理[未登錄]
不錯
--aa
4.?re: jQuery基礎ready
不錯!
有個小小建議,展示代碼時請不要使用‘行號’,這樣讀者才能方便地其驗證正確性并反饋 ;)
--山風小子
5.?re: Sql Server 調用存儲過程
寶貝加油,你是最棒的!不要灰心,前途是光明的!!!
--kay
添加動態驗證碼
Posted on 2007-07-25 14:26
胡娟
閱讀(716)
評論(1)
編輯
收藏
1.用戶信息VO包
1
package
com.hujuan.VO;
2
3
public
class
UserInfo
{
4
private
String username;
5
private
String password;
6
public
String getPassword()
{
7
return
password;
8
}
9
public
void
setPassword(String password)
{
10
this
.password
=
password;
11
}
12
public
String getUsername()
{
13
return
username;
14
}
15
public
void
setUsername(String username)
{
16
this
.username
=
username;
17
}
18
}
2.驗證登錄Dao包
1
package
com.hujuan.dao;
2
3
import
com.hujuan.VO.UserInfo;
4
5
public
class
UserInfoDao
{
6
public
boolean
checklogin(UserInfo userinfo)
{
7
System.out.println(userinfo.getUsername());
8
System.out.println(userinfo.getUsername());
9
if
(
"
hujuan
"
.equals(userinfo.getUsername())
&&
"
123
"
.equals(userinfo.getPassword()))
{
10
return
true
;
11
}
return
false
;
12
}
13
}
3.生產隨機碼驗證servlet
1
package
com.hujuan.servlet;
2
3
4
import
java.awt.Graphics2D;
5
import
java.awt.image.BufferedImage;
6
import
java.io.IOException;
7
import
java.util.Random;
8
import
java.awt.Color;
9
import
java.awt.Font;
10
import
javax.imageio.ImageIO;
11
import
javax.servlet.ServletException;
12
import
javax.servlet.ServletOutputStream;
13
import
javax.servlet.http.HttpServlet;
14
import
javax.servlet.http.HttpServletRequest;
15
import
javax.servlet.http.HttpServletResponse;
16
import
javax.servlet.http.HttpSession;
17
18
public
class
RandomCodeServler
extends
HttpServlet
{
19
private
int
width
=
60
;
//
驗證圖片的寬度
20
private
int
height
=
20
;
//
驗證圖片的高度
21
@Override
22
protected
void
service(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException
{
23
24
BufferedImage buffImg
=
new
BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
25
26
Graphics2D g
=
buffImg.createGraphics();
27
28
29
//
創建一個隨機數生產器類
30
Random random
=
new
Random();
31
32
g.setColor(Color.WHITE);
33
g.fillRect(
0
,
0
, width, height);
34
35
//
創建字體,字體的大小應該根據圖片的高度來定
36
Font font
=
new
Font(
"
123123
"
,Font.PLAIN,
18
);
37
//
設置字體
38
g.setFont(font);
39
//
畫邊框
40
g.setColor(Color.BLACK);
41
g.drawRect(
0
,
0
, width
-
1
,height
-
1
);
42
//
隨機生產100條干擾線,使圖像中的驗證嗎不易被其他程序探測到
43
g.setColor(Color.GRAY);
44
for
(
int
i
=
0
;i
<
100
;i
++
)
{
45
int
x
=
random.nextInt(width);
46
int
y
=
random.nextInt(height);
47
int
x1
=
random.nextInt(
12
);
48
int
y1
=
random.nextInt(
12
);
49
g.drawLine(x, y, x
+
x1, y
+
y1);
50
}
51
//
randomCode 用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證
52
StringBuffer randomCode
=
new
StringBuffer();
53
int
red
=
0
,green
=
0
,blue
=
0
;
54
//
隨機產生4位數字的驗證碼
55
for
(
int
i
=
0
;i
<
4
;i
++
)
{
56
//
得到隨機產生的驗證碼數字
57
String strRand
=
String.valueOf(random.nextInt(
10
));
58
59
//
產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同
60
red
=
random.nextInt(
110
);
61
green
=
random.nextInt(
50
);
62
blue
=
random.nextInt(
50
);
63
64
//
用隨機產生的顏色將驗證碼繪制到圖像中
65
g.setColor(
new
Color(red,green,blue));
66
g.drawString(strRand,
13
*
i
+
6
,
16
);
67
//
將產生的四個隨機數組合在一起
68
randomCode.append(strRand);
69
}
70
//
將四位數字的驗證碼保存到session中
71
HttpSession session
=
req.getSession();
72
session.setAttribute(
"
randomCode
"
, randomCode.toString());
73
//
禁止圖像緩存
74
//
resp.setHeader("Pragma", "no-cache");
75
//
resp.setHeader("Cache-Control", "no-cache");
76
//
resp.setDateHeader("Expires", 0);
77
//
resp.setContentType("image/jpeg");
78
//
將圖像輸出到servlet輸出流中
79
ServletOutputStream out
=
resp.getOutputStream();
80
ImageIO.write(buffImg,
"
jpeg
"
,out);
81
out.close();
82
}
83
}
84
4.驗證登錄的servlet
1
package
com.hujuan.servlet;
2
3
import
java.io.IOException;
4
import
java.io.PrintWriter;
5
import
javax.servlet.ServletException;
6
import
javax.servlet.http.HttpServlet;
7
import
javax.servlet.http.HttpServletRequest;
8
import
javax.servlet.http.HttpServletResponse;
9
import
javax.servlet.http.HttpSession;
10
import
com.hujuan.VO.UserInfo;
11
import
com.hujuan.dao.UserInfoDao;
12
13
public
class
LoginCheckServlet
extends
HttpServlet
{
14
15
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
16
throws
ServletException, IOException
{
17
18
//
request.setCharacterEncoding("gb2312");
19
response.setCharacterEncoding(
"
gb2312
"
);
20
21
response.setContentType(
"
text/html; charset = gb2312
"
);
22
PrintWriter out
=
response.getWriter();
23
24
HttpSession session
=
request.getSession();
25
String randomCode
=
(String)session.getAttribute(
"
randomCode
"
);
26
27
if
(
null
==
randomCode)
{
28
response.sendRedirect(
"
index.jsp
"
);
29
return
;
30
}
31
32
String reqRandom
=
request.getParameter(
"
randomcode
"
);
//
meiyou huode
33
34
UserInfo userinfo
=
new
UserInfo();
35
String name
=
request.getParameter(
"
username
"
);
36
String password
=
request.getParameter(
"
password
"
);
37
38
userinfo.setUsername(name);
39
userinfo.setPassword(password);
40
41
42
//
System.out.println(reqRandom);
43
//
System.out.println(randomCode);
44
//
System.out.println(name);
45
//
System.out.println(password);
46
if
(randomCode.equals(reqRandom))
{
47
UserInfoDao userdao
=
new
UserInfoDao();
48
if
(userdao.checklogin(userinfo))
{
49
response.sendRedirect(
"
succ.jsp
"
);
50
}
else
{
51
out.println(
"
請檢查用戶名和密碼
"
);
52
}
53
}
else
{
54
out.println(
"
請檢查驗證碼
"
);
55
}
56
}
57
58
}
59
5.登錄頁面
1
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
2
contentType
=
"
text/html; charset=gb2312
"
%>
3
<%
4
String path
=
request.getContextPath();
5
String basePath
=
request.getScheme()
+
"
://
"
6
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
7
+
path
+
"
/
"
;
8
%>
9
10
<!
DOCTYPE HTML PUBLIC
"
-//W3C//DTD HTML 4.01 Transitional//EN
"
>
11
<
html
>
12
<
head
>
13
<
base href
=
"
<%=basePath%>
"
>
14
15
<
title
>
My JSP
'
Login.jsp
'
starting page
</
title
>
16
17
<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
18
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
19
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
20
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
21
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
22
<!--
23
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
24
-->
25
26
<
style type
=
"
text/css
"
>
27
<!--
28
.text
{
29
font
-
family:
"
宋體
"
;
30
font
-
size: 12px;
31
font
-
style: normal;
32
line
-
height: 25px;
33
font
-
weight: normal;
34
font
-
variant: normal;
35
color: #
000000
;
36
}
37
-->
38
</
style
>
39
</
head
>
40
41
<
body
>
42
<
form method
=
"
post
"
name
=
"
login
"
action
=
"
LoginCheckServlet
"
>
43
<
p
>&
nbsp;
44
45
</
p
>
46
<
table width
=
"
40%
"
border
=
"
0
"
align
=
"
center
"
>
47
<
tbody
>
48
<
tr
>
49
<
td width
=
"
22%
"
align
=
"
right
"
class
=
"
text
"
>&
nbsp;用戶名
</
td
>
50
<
td width
=
"
37%
"
><
input name
=
"
username
"
type
=
"
text
"
size
=
"
20
"
>
51
<
br
>
52
</
td
>
53
</
tr
>
54
<
tr
>
55
<
td align
=
"
right
"
class
=
"
text
"
>&
nbsp;密 碼
</
td
>
56
<
td
><
input name
=
"
password
"
type
=
"
password
"
size
=
"
20
"
maxlength
=
"
16
"
>
57
</
td
>
58
</
tr
>
59
<
tr
>
60
<
td align
=
"
right
"
class
=
"
text
"
>&
nbsp;驗證碼
</
td
>
61
<
td
><
input name
=
"
randomcode
"
type
=
"
text
"
size
=
"
5
"
maxlength
=
"
4
"
>
62
<
img src
=
"
imgcode
"
>
</
td
>
63
</
tr
>
64
<
tr
>
65
<
td align
=
"
right
"
>&
nbsp;
66
<
input type
=
"
submit
"
value
=
"
提交
"
name
=
"
submit
"
>
</
td
>
67
<
td align
=
"
center
"
>&
nbsp;
68
<
input type
=
"
reset
"
value
=
"
重置
"
name
=
"
reset
"
>
</
td
>
69
</
tr
>
70
</
tbody
>
71
</
table
>
72
</
form
>
73
</
body
>
74
</
html
>
75
6.成功頁面
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
>
My JSP
'
succ.jsp
'
starting page
</
title
>
13
14
<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
15
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
16
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
17
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
18
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
19
<!--
20
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
21
-->
22
23
</
head
>
24
25
<
body
>
26
This is my JSP page.
<
br
>
27
</
body
>
28
</
html
>
29
Feedback
#
re: 添加動態驗證碼
回復
更多評論
2012-05-27 22:04 by
mable
我覺得很奇怪,為什么我的驗證碼總是“62”呢?勞煩給我解釋一下servlet繪制的驗證碼圖片是怎么放在登陸頁面的啊? <img src="imgcode">,imgcode從哪里來?
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
公告
www.kaishengit.com
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
我的鏈接
JavaOnRoad
積分與排名
積分 - 24597
排名 - 1548
閱讀排行榜
1.?Sql Server 調用存儲過程(2799)
2.?JPA+Hibernate 3.3 學習小結——雙向多對一關聯及級聯操作(2460)
3.?JPA+Hibernate 3.3 學習小結——第一個JPA程序(2030)
4.?JDBC 工作原理(1890)
5.?JPA+Hibernate 3.3 學習小結——使用JQL語句(1574)
posts - 28, comments - 5, trackbacks - 0, articles - 1
Copyright © 胡娟
主站蜘蛛池模板:
好爽好紧好大的免费视频国产
|
3344在线看片免费
|
亚洲人成色4444在线观看
|
亚洲国产成AV人天堂无码
|
亚洲精品在线播放视频
|
亚洲一级二级三级不卡
|
97久久精品亚洲中文字幕无码
|
中文字幕无码毛片免费看
|
jizz免费观看
|
在线观看人成视频免费无遮挡
|
无码 免费 国产在线观看91
|
深夜福利在线免费观看
|
一级大黄美女免费播放
|
中文字幕免费观看全部电影
|
国产午夜无码精品免费看动漫
|
a毛片免费全部在线播放**
|
久久精品免费电影
|
真人做A免费观看
|
免费看无码自慰一区二区
|
免费jlzzjlzz在线播放视频
|
亚洲男人在线无码视频
|
亚洲精品成人无限看
|
久久国产精品亚洲综合
|
亚洲噜噜噜噜噜影院在线播放
|
亚洲最大中文字幕无码网站
|
国产成人亚洲精品蜜芽影院
|
久久不见久久见免费影院www日本
|
三年片在线观看免费
|
91网站免费观看
|
免费看无码自慰一区二区
|
久久久久亚洲av毛片大
|
亚洲AV无码欧洲AV无码网站
|
亚洲伊人久久精品
|
国产精品亚洲一区二区三区在线观看
|
亚洲日本一区二区三区
|
亚洲伊人久久大香线蕉啊
|
在线看亚洲十八禁网站
|
国产真人无码作爱视频免费
|
成人无码区免费视频观看
|
最好看的中文字幕2019免费
|
女人毛片a级大学毛片免费
|