Chinaunix

标题: 哪位作过验证码程序啊??JSP的!! [打印本页]

作者: javafun    时间: 2004-03-22 15:02
标题: 哪位作过验证码程序啊??JSP的!!
哪位作过验证码程序啊??JSP的!!类似下面的:能将代码提供给大家看看吗??
BufferedImage image = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
Graphics g = image.getGraphics();

// 设定背景色
g.setColor(Color.white);
g.fillRect(0, 0, width, height);

//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);

// 取随机产生的认证码(4位数字)
String rand = request.getParameter("rand";
rand = rand.substring(0,rand.indexOf(".");
switch(rand.length())
{
case 1: rand = "000"+rand; break;
case 2: rand = "00"+rand; break;
case 3: rand = "0"+rand; break;
default: rand = rand.substring(0,4); break;
}

// 将认证码存入SESSION。。。。

// 将认证码显示到图象中
g.setColor(Color.black);
g.setFont(new Font("Times New Roman",Font.PLAIN,1);
g.drawString(rand,10,15);

// 随机产生干扰点
Random random = new Random();
for (int i=0;i<88;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawLine(x,y,x,y);
}

// 图象生效
g.dispose();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());

=======================================
调用的时候用脚本写
document.write("<img border=0 src=image.jsp?
rand="+Math.random()*10000+">;";
作者: SunLife    时间: 2004-03-22 15:05
标题: 哪位作过验证码程序啊??JSP的!!
不一定要画图,其实您只需要打开别的网站的源码看看就会发现
譬如sun.com.cn就是使用了四个小图片,依靠随机数来调用不同的图片:)
而Chinaren.com则是用的php的画图:)

我做过的验证码就是依靠随机数随机调用10个小图片中的四个:)
作者: javafun    时间: 2004-03-27 10:17
标题: 哪位作过验证码程序啊??JSP的!!
那能不能将你的代码贴出来看一下啊??谢谢!!!
作者: SunLife    时间: 2004-03-28 17:53
标题: 哪位作过验证码程序啊??JSP的!!
对不起,我觉得我是新手,很新的,都会写,如果您写不出来,那就不好说什么了,很基础的。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2