验证码的产生,大体思路是在Servlet中产生随机数,然后用Graphic 的draw方法将这些随机数画出来,图片返回到页面,随机数放在session里,以便验证。在jsp页面中,只要重写图片src即可实现刷新。
实现验证码的servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
public
class
imageServlet
extends
HttpServlet {
private
static
final
long
serialVersionUID = 1L;
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doPost(request, response);
}
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
int
width=
150
;
int
height=
60
;
BufferedImage image=
new
BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
Graphics g=image.getGraphics();
Random random=
new
Random();
g.setColor(getRandColor(
200
,
250
));
g.fillRect(
0
,
0
, width, height);
g.setColor(getRandColor(
0
,
255
));
g.drawRect(
0
,
0
, width-
1
, height-
1
);
g.setColor(getRandColor(
160
,
200
));
for
(
int
i=
0
;i<
8
;i++){
int
x=random.nextInt(width);
int
y=random.nextInt(height);
int
x1=random.nextInt(width);
int
y1=random.nextInt(height);
g.drawLine(x, y, x1, y1);
}
g.setColor(getRandColor(
160
,
200
));
for
(
int
i=
0
;i<
100
;i++){
int
x=random.nextInt(width);
int
y=random.nextInt(height);
g.drawLine(x, y, x, y);
}
Font font =
new
Font(
"Times New Roman"
, Font.ITALIC,
38
);
g.setFont(font);
int
length =
6
;
String s=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
;
String sRand=
""
;
g.setColor(
new
Color(
20
+random.nextInt(
110
),
20
+random.nextInt(
110
),
20
+random.nextInt(
110
)));
for
(
int
i=
0
;i<length;i++){
String ch=String .valueOf(s.charAt(random.nextInt(s.length())));
sRand+=ch;
g.drawString(ch,
22
*i+
12
, (random.nextInt(
5
)-
2
)*i+
40
);
}
HttpSession session=request.getSession();
session.setAttribute(
"checkCode"
, sRand);
g.dispose();
response.setHeader(
"Pragma"
,
"No-cache"
);
response.setHeader(
"Cache-Control"
,
"no-cache"
);
response.setDateHeader(
"Expires"
,
0
);
response.setContentType(
"image/jpeg"
);
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(image,
"jpeg"
, sos);
sos.flush();
sos.close();
}
public
Color getRandColor(
int
lower,
int
upper){
Random random =
new
Random();
if
(upper>
255
)
upper=
255
;
if
(upper<
1
)
upper=
1
;
if
(lower<
1
)
lower=
1
;
if
(lower>
255
)
lower=
255
;
int
r=lower+random.nextInt(upper-lower);
int
g=lower+random.nextInt(upper-lower);
int
b=lower+random.nextInt(upper-lower);
return
new
Color(r,g,b);
}
}
|
在页面中的调用
1
|
<
span
id
=
"myspan"
><
img
src
=
"imageServlet"
name
=
"img1"
id
=
"img1"
alt
=
""
width
=
"75"
height
=
"24"
/><
a
href
=
"javascript:reloadImage()"
>刷新</
a
></
span
>
|
使用javascript进行刷新
1
2
3
4
5
6
7
|
function
reloadImage(){
var
date =
new
Date();
var
t =date.getTime();
var
a=document.getElementById(
"myspan"
);
a.innerHTML=
'<img src="imageServlet?a='
+t +
'name="img1" id="img1" alt="" width="75" height="24" /><a href="javascript:reloadImage()" >刷新</a>'
;
}
|
需要注意的是,由于在浏览器中,如果刷新图片的src完全相同,浏览器将不会访问新的src而是直接使用缓存中的内容,因此,将src设为imageServlet?a=.....是为了保证每次刷新图片都访问不同的地址。