JSP图片验证代码


验证码应用在各种场合中,十分广泛,在多种多样的系统或软件中的新用户帐户注册、用户登录、网站统一登陆或者用户在网站发布信息模块发布文章或内容都添加的随机码功能,对用户操作过程进行一种验证,使用验证码的目的就是为了避免网络中的自动注册程序或者自动发布程序的滥用。 验证码的原理其实就是随机选择一些字符码以将字符码以图片的形式展现在软件验证界面或验证页面上,当用户在进行提交操作的同时需要将图片上的验证码输入并同时提交,如果提交的字符验证码与服务器session保存的字符码相同,则认为提交信息有效,否则拒绝提交。在使用验证码过程中,为了避免自动分析程序解析图片并获得验证信息,通常需要在图片上随机生成一些干扰线或者将复杂的字符对图片进行扭曲模糊,从而增加了自动识别程序分析验证图片的难度。

JSP验证码源码大全将分别用几个篇幅的内容来介绍在JSP中几种验证码的实现源码以及使用。

一、JSP中产生数字验证码源码

数字验证码是一种最常用的验证字符码形式,以下为数字实现的JSP源码:

Num.jsp
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!

Color getRandColor(int cc,int bb)

{

Random random = new Random();

if(fc>255) cc=255;

if(bc>255) bb=255;

int r=cc+random.nextInt(bb-cc);

int g=cc+random.nextInt(bb-cc);

int b=cc+random.nextInt(bb-cc);

returnnew Color(r,g,b);

} //获取随机颜色


%>

<%

response.setHeader("Pragma","No-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires", 0);

int width=80; //定义验证码图片的长度

int height=30; //定义验证码图片的宽度

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(getRandColor(160,200));

for (int i=0;i<155;i++)

{

int i_x = random.nextInt(width);

int i_y = random.nextInt(height);

int i_xl = random.nextInt(12);

int i_yl = random.nextInt(12);

g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);

}


//用线条画背景


String s_Rand="";

for (int i=0;i<4;i++)

{

String rand=String.valueOf(random.nextInt(10));

s_Rand+=rand;



g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));

g.drawString(rand,

13*i+6,16);

}


//产生4位随机码


session.setAttribute("rand",s_Rand);


//将验证码存入Session中


g.dispose();



ImageIO.write(image, "JPEG", response.getOutputStream());


//输出验证图片


out.clear();

out = pageContext.pushBody();



%>


JSP图片验证代码

JSP生成图片验证码代码代码如下:

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("captcha", sRand);

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

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

out.clear();
out = pageContext.pushBody();

%>
一定不要漏掉红色部分,否则会报出错误:
getOutputStream() has already been called for this response

在需要使用验证码的地方加入:





相关文档
最新文档