{image}

如何制作 java web 验证码

java web的验证码原理如下: 根据参数随机生成一个字符串 ,将字符串存储在一个静态变量中,然后将字符串写在图片上传递到前台供用户识别。用户将识别出的字符串再提交到服务器, 服务器取出静态变量中的字符串与之对比 。然后将对比结果返回给用户。
可以自己在网上找些实现好的代码学习一下 ,也可以使用一些比较成熟的验证码组件。
随着越来越发达的网络技术, 验证码技术也在不断进步,有的验证码中的字符变成了中文 ,有的在图片中生成问题来让用户回答 ,有的甚至用到了图片归类,但原理没变 。用rondom获得随机数字,用drawstring画出来原理是这样 ,写着很麻烦,网上有给你个实例:网站找的,具体在哪找的我忘记了。
<%@ page contenttype="image/jpeg" import=".,.,.,." pageencoding="utf-8"%>
<%!
public color getrandcolor(int fc,int bc){//给定范围获得随机颜色
random random = new random();
if(fc>255){
fc=255;
}
if(bc>255){
bc=255;
}
int r=fc+(bc-fc);
int g=fc+(bc-fc);
int b=fc+(bc-fc);
return new color(r,g,b);
}
%><%
//设置页面不缓存
("pragma","no-cache");
("cache-control","no-cache");
ader("expires", 0);
// 在内存中创建图象
int width=60, height=19;
bufferedimage image = new bufferedimage(width, height, _int_rgb);
// 获取图形上下文
graphics g = cs();
//生成随机类
random random = new random();
// 设定背景色
(getrandcolor(150,250));
(0, 0, width, height);
//设定字体
(new font("times new roman",,18));
//画边框
//(new color());
//(0,0,width-1,height-1);
// 随机产生155条干扰线 ,使图象中的认证码不易被其它程序探测到
(getrandcolor(160,200));
for (int i=0;i<155;i++)
{
int x = (width);
int y = (height);
int xl = (12);
int yl = (12);
(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
string srand="";
for (int i=0;i<4;i++){
string rand=((10));
srand+=rand;
// 将认证码显示到图象中
(new color(20+(110),20+(110),20+(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g(rand,13i+6,16);
}
// 将认证码存入session
ute("rand",srand);
// 图象生效
();
// 输出图象到页面
(image, "jpeg",stream());
stream().flush();
stream().close();
er();
();
out = ();
%>给你个实例:网站找的 ,具体在哪找的我忘记了。
<%@ page contentType="image/jpeg" import=".
,.,.,." pageEncoding="utf-8"%>
<%!
public Color getRandColor(int fc,int bc){//给定范围获得随机颜色
Random random = new Random();
if(fc>255){
fc=255;
}
if(bc>255){
bc=255;
}
int r=fc+(bc-fc);
int g=fc+(bc-fc);
int b=fc+(bc-fc);
return new Color(r,g,b);
}
%><%
//设置页面不缓存
("Pragma","No-cache");
("Cache-Control","no-cache");
ader("Expires", 0);
// 在内存中创建图象
int width=60, height=19;
BufferedImage image = new BufferedImage(width, height, _INT_RGB);
// 获取图形上下文
Graphics g = cs();
//生成随机类
Random random = new Random();
// 设定背景色
(getRandColor(150,250));
(0, 0, width, height);
//设定字体
(new Font("Times New Roman",,18));
//画边框
//(new Color());
//(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = (width);
int y = (height);
int xl = (12);
int yl = (12);
(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
String rand=((10));
sRand+=rand;
// 将认证码显示到图象中
(new Color(20+(110),20+(110),20+(110)));//调用函数出来的颜色相同,可能是因为种子太接近 ,所以只能直接生成
g(rand,13
i+6,16);
}
// 将认证码存入SESSION
ute("rand",sRand);
// 图象生效
();
// 输出图象到页面
(image, "JPEG",Stream());
Stream().flush();
Stream().close();
er();
();
out = ();
%>

本文版权归去快排wWw.seogUrublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系qq❉61910465