生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新(用history.go(-1)也会变)
产生验证码图片的文件image.jsp
<%@ page contenttype="image/jpeg" import="java.awt.*, java.awt.image.*,java.util.*,javax.imageio.*" %> <%! color getrandcolor(int fc,int bc){//给定范围获得随机颜色 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); } %> <% //设置页面不缓存 response.setheader("pragma","no-cache"); response.setheader("cache-control","no-cache"); response.setdateheader("expires", 0); // 在内存中创建图象 int width=60, height=20; bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); // 获取图形上下文 graphics g = image.getgraphics(); //生成随机类 random random = new random(); // 设定背景色 g.setcolor(getrandcolor(200,250)); g.fillrect(0, 0, width, height); //设定字体 g.setfont(new font("times new roman",font.plain,18)); //画边框 //g.setcolor(new color()); //g.drawrect(0,0,width-1,height-1); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 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); } // 取随机产生的认证码(4位数字) string srand=""; for (int i=0;i<4;i++){ string rand=string.valueof(random.nextint(10)); srand+=rand; // 将认证码显示到图象中 g.setcolor(new color(20+random.nextint(110),20+random.nextint(110),20+random.nextint(110))); //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawstring(rand,13*i+6,16); } // 将认证码存入session session.setattribute("rand",srand); // 图象生效 g.dispose(); // 输出图象到页面 imageio.write(image, "jpeg", response.getoutputstream()); %> |
<%@ page contenttype="text/html;charset=gb2312" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>认证码输入页面</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>系统产生的认证码:</td> <td><img border=0 src="image.jsp"></td> </tr> <tr> <td align=left>输入上面的认证码:</td> <td><input type=text name=rand maxlength=4 value=""></td> </tr> <tr> <td colspan=2 align=center><input type=submit value="提交检测"></td> </tr> </form> </body> </html> |
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %> <html> <head> <title>认证码验证页面</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"); %> 系统产生的认证码为: <%= rand %><br> 您输入的认证码为: <%= input %><br> <br> <% if (rand.equals(input)) { %> <font color=green>输入相同,认证成功!</font> <% } else { %> <font color=red>输入不同,认证失败!</font> <% } %> </body> </html> |
新闻热点
疑难解答