JSP彩色验证码

生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新(用history.go(-1)也会变)
原型参考ALIBABA http://china.alibaba.com/member/showimage

产生验证码图片的文件-----image.jsp

1@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" 
 1!   
 2Color getRandColor(int fc,int bc){//给定范围获得随机颜色   
 3Random random = new Random();   
 4if(fc>255) fc=255;   
 5if(bc>255) bc=255;   
 6int r=fc+random.nextInt(bc-fc);   
 7int g=fc+random.nextInt(bc-fc);   
 8int b=fc+random.nextInt(bc-fc);   
 9return new Color(r,g,b);   
10}   
 1   
 2//设置页面不缓存   
 3response.setHeader("Pragma","No-cache");   
 4response.setHeader("Cache-Control","no-cache");   
 5response.setDateHeader("Expires", 0); 
 6
 7// 在内存中创建图象   
 8int width=60, height=20;   
 9BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
10
11// 获取图形上下文   
12Graphics g = image.getGraphics(); 
13
14//生成随机类   
15Random random = new Random(); 
16
17// 设定背景色   
18g.setColor(getRandColor(200,250));   
19g.fillRect(0, 0, width, height); 
20
21//设定字体   
22g.setFont(new Font("Times New Roman",Font.PLAIN,18)); 
23
24//画边框   
25//g.setColor(new Color());   
26//g.drawRect(0,0,width-1,height-1); 
27
28  
29// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
30g.setColor(getRandColor(160,200));   
31for (int i=0;i<155;i++)   
32{   
33int x = random.nextInt(width);   
34int y = random.nextInt(height);   
35int xl = random.nextInt(12);   
36int yl = random.nextInt(12);   
37g.drawLine(x,y,x+xl,y+yl);   
38} 
39
40// 取随机产生的认证码(4位数字)   
41String sRand="";   
42for (int i=0;i<4;i++){   
43String rand=String.valueOf(random.nextInt(10));   
44sRand+=rand;   
45// 将认证码显示到图象中   
46g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
47g.drawString(rand,13*i+6,16);   
48} 
49
50// 将认证码存入SESSION   
51session.setAttribute("rand",sRand); 
52
53  
54// 图象生效   
55g.dispose(); 
56
57// 输出图象到页面   
58ImageIO.write(image, "JPEG", response.getOutputStream()); 
59
60  

---------------使用验证码图片的文件---------a.jsp------------------------------------

1@ page contentType="text/html;charset=gb2312" 
 1<html>
 2<head>
 3<title>认证码输入页面</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5<meta content="no-cache" http-equiv="Pragma"/>
 6<meta content="no-cache" http-equiv="Cache-Control"/>
 7<meta content="0" http-equiv="Expires"/>
 8</head>
 9<body>
10<form action="check.jsp" method="post">
11<table>
12<tr>
13<td align="left">系统产生的认证码:</td>
14<td><img border="0" src="image.jsp"/></td>
15</tr>
16<tr>
17<td align="left">输入上面的认证码:</td>
18<td><input maxlength="4" name="rand" type="text" value=""/></td>
19</tr>
20<tr>
21<td align="center" colspan="2"><input type="submit" value="提交检测"/></td>
22</tr>
23</table></form>
24</body>
25</html>

-----------------验证的页面----------check.jsp

1@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" 
1<html>
2<head>
3<title>认证码验证页面</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
5<meta content="no-cache" http-equiv="Pragma"/>
6<meta content="no-cache" http-equiv="Cache-Control"/>
7<meta content="0" http-equiv="Expires"/>
8</head>
9<body>   

String rand = (String)session.getAttribute("rand");
String input = request.getParameter("rand");

1系统产生的认证码为: ```
2= rand 
3```<br/>   
4您输入的认证码为: ```
5= input 
6```<br/>
7<br/>   

if (rand.equals(input)) {

1<font color="green">输入相同,认证成功!</font>   

} else {

1<font color="red">输入不同,认证失败!</font>   

}

1</body>
2</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus