一个登陆模块。想输入验证码进行验证!一直难道我,请教高手指点
解决方案 »
- 我是菜鸟,诚心请教jsp+javabean+servlet的问题!
- Servlet类的个数是多好还是少好?
- 关于前后台交互的一个问题
- jar在项目中的位置
- 请问request.getParameterValues()这个方法,获取数组里面的值,顺序是不是和页面上的一样呢?
- ResultSetMetaData的getTableName()返回的表明总是空值怎么解决?
- 大家好,刚开始学hibernate,我想问问各位谁有Hibernate.cfg.xml语意的中文说明以及*.hbm.xml的语意说明
- 一个奇怪的问题,郁闷中。。。大家发表一下.
- 再探讨Tomcat5.0的server.xml配置问题!!
- 请教如何用JAVA/JSP实现树形结构(一定给分)
- 分页显示问题!数据库里面的表分页显示在浏览器中
- hibernate配置单元测试时能加载,web跑起来就报错,求解
throws ServletException, IOException {
int height = 20;
int width = 60;
//创造一个图片缓冲流,包含所有的图片信息,相当于画布
BufferedImage buimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buimg.createGraphics();//创建一个绘图工具
g.setColor(Color.WHITE);//设置底色
g.fillRect(0, 0, width, height);//开始填充
g.setColor(Color.RED);//设置边框颜色
g.drawRect(0, 0, width-1, height-1);//绘制边框
Random rand = new Random();
g.setColor(Color.GRAY);//设置干扰线的颜色
for (int i = 1; i <= 20; i++) {
int x1 = rand.nextInt(width);
int y1 = rand.nextInt(height);
int x2 = rand.nextInt(width);
int y2 = rand.nextInt(height);
g.drawLine(x1, y1, x2, y2);//绘制干扰线
}
String code = "";//用来记录生成的验证码
int red=0,green=0,blue=0;//记录三原色的值,用来变换验证码的颜色
Font f = new Font("Times New Roman",Font.BOLD,18);//创建一个字体对象
g.setFont(f);//设置验证码的字体
for (int i = 1; i <= 4; i++) {
String num = rand.nextInt(10)+"";//随机生成验证码中的一个数字
// System.out.println(num);
//随机生成三原色
red = rand.nextInt(100);
green = rand.nextInt(100);
blue = rand.nextInt(100);
g.setColor(new Color(red,green,blue));//设置验证码的颜色
g.drawString(num,11*i,16);//绘制验证码
code+=num;//拼接四个验证码到一起
}
// System.out.println(code);
HttpSession session = request.getSession();
session.setAttribute("checkcode", code);//将验证码放入到session中,以便于登录检查使用
//获得输出流,把图片发送给IE
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(buimg,"jpeg", sos);//开始发送
sos.flush();
sos.close();//关闭流
}
这个是在Servlet里面写的都有注释,看不看得明白?
<script type="text/javascript">
function changImg(img)
{
img.src = "checkcode?"+new Date().getTime();
}
</script>这个是JSP页面的,看到那个IMG里面的src没有?路径就是servlet的名字。。
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();
OutputStream output = response.getOutputStream(); // 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
output.flush();
out.clear();
out = pageContext.pushBody();
%>