JSP生成验证码源程序
程序员文章站
2022-06-09 17:13:41
<%@ page contenttype="image/jpeg" import="java.awt.*, java.awt.image.*,java.util.*,java...
<%@ page contenttype="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
// 在内存中创建图象
int width=60, height=20;
bufferedimage image = new bufferedimage(width, height,
bufferedimage.type_int_rgb);
// 获取图形上下文
graphics g = image.getgraphics();
// 设定背景色
g.setcolor(new color(0xdcdcdc));
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
session.setattribute("rand",rand);
// 将认证码显示到图象中
g.setcolor(color.black);
integer tempnumber = new integer(rand);
string numberstr = tempnumber.tostring();
g.setfont(new font("atlantic inline",font.plain,18));
string str = numberstr.substring(0,1);
g.drawstring(str,8,17);
str = numberstr.substring(1,2);
g.drawstring(str,20,15);
str = numberstr.substring(2,3);
g.drawstring(str,35,18);
str = numberstr.substring(3,4);
g.drawstring(str,45,15);
// 随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
random random = new random();
for (int i=0;i<20;i++)
{
int x = random.nextint(width);
int y = random.nextint(height);
g.drawoval(x,y,0,0);
}
// 图象生效
g.dispose();
// 输出图象到页面
imageio.write(image, "jpeg", response.getoutputstream());
%>
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
// 在内存中创建图象
int width=60, height=20;
bufferedimage image = new bufferedimage(width, height,
bufferedimage.type_int_rgb);
// 获取图形上下文
graphics g = image.getgraphics();
// 设定背景色
g.setcolor(new color(0xdcdcdc));
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
session.setattribute("rand",rand);
// 将认证码显示到图象中
g.setcolor(color.black);
integer tempnumber = new integer(rand);
string numberstr = tempnumber.tostring();
g.setfont(new font("atlantic inline",font.plain,18));
string str = numberstr.substring(0,1);
g.drawstring(str,8,17);
str = numberstr.substring(1,2);
g.drawstring(str,20,15);
str = numberstr.substring(2,3);
g.drawstring(str,35,18);
str = numberstr.substring(3,4);
g.drawstring(str,45,15);
// 随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
random random = new random();
for (int i=0;i<20;i++)
{
int x = random.nextint(width);
int y = random.nextint(height);
g.drawoval(x,y,0,0);
}
// 图象生效
g.dispose();
// 输出图象到页面
imageio.write(image, "jpeg", response.getoutputstream());
%>
上一篇: TChart-序列类工具功能演示
下一篇: TChart-图表的滚动与翻译