使用JSP生成验证码

在开发中验证码是比较常用到有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试的方式。
此演示程序包括三个文件:
1.index.jsp:登录页面
2.image.jsp:生成验证码图片页面
3.result.jsp:结果页面
 
【页面显示】
图片加载不成功
【页面代码】
1.index.jsp
 
xml 代码
  1. <html><body>      
  2. <form method=post action="result.jsp">      
  3. <input type=text name=input maxlength=4>      
  4. <img border=0 src="image.jsp">      
  5. <input type="submit" value="submit">      
  6. </form></body></html>  
[注意]:
(1)使用maxlength属性来限制输入字符;
(2)使用<img>标签来显示生成的验证码图片.
 
2.image.jsp
  1. <%@ page contentType="image/JPEG"  
  2.     import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"  
  3.     pageEncoding="GBK"%>  
  4. <%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜色   
  5.         Random random = new Random();   
  6.         if (fc > 255)   
  7.             fc = 255;   
  8.         if (bc > 255)   
  9.             bc = 255;   
  10.         int r = fc + random.nextInt(bc - fc);   
  11.         int g = fc + random.nextInt(bc - fc);   
  12.         int b = fc + random.nextInt(bc - fc);   
  13.         return new Color(r, g, b);   
  14.     }%>  
  15. <%   
  16.     //设置页面不缓存   
  17.     response.setHeader("Pragma", "No-cache");   
  18.     response.setHeader("Cache-Control", "no-cache");   
  19.     response.setDateHeader("Expires", 0);   
  20.   
  21.     // 在内存中创建图象   
  22.     int width = 60height = 20;   
  23.     BufferedImage image = new BufferedImage(width, height,   
  24.             BufferedImage.TYPE_INT_RGB);   
  25.   
  26.     // 获取图形上下文   
  27.     Graphics g = image.getGraphics();   
  28.   
  29.     //生成随机类   
  30.     Random random = new Random();   
  31.   
  32.     // 设定背景色   
  33.     g.setColor(getRandColor(200, 250));   
  34.     g.fillRect(0, 0, width, height);   
  35.   
  36.     //设定字体   
  37.     g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   
  38.   
  39.     //画边框   
  40.     //g.setColor(new Color());   
  41.     //g.drawRect(0,0,width-1,height-1);   
  42.   
  43.     // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
  44.     g.setColor(getRandColor(160, 200));   
  45.     for (int i = 0; i < 100; i++) {   
  46.         int x = random.nextInt(width);   
  47.         int y = random.nextInt(height);   
  48.         int xl = random.nextInt(12);   
  49.         int yl = random.nextInt(12);   
  50.         g.drawLine(x, y, x + xl, y + yl);   
  51.     }   
  52.   
  53.     // 取随机产生的认证码(4位数字)   
  54.     String sRand = "";   
  55.     for (int i = 0; i < 4; i++) {   
  56.         String rand = String.valueOf(random.nextInt(10));   
  57.         sRand += rand;   
  58.         // 将认证码显示到图象中   
  59.         g.setColor(new Color(20 + random.nextInt(110), 20 + random   
  60.         .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
  61.         g.drawString(rand, 13 * i + 6, 16);   
  62.     }   
  63.   
  64.     // 将认证码存入SESSION   
  65.     session.setAttribute("code", sRand);   
  66.   
  67.     // 图象生效   
  68.     g.dispose();   
  69.   
  70.     // 输出图象到页面   
  71.     ImageIO.write(image, "JPEG", response.getOutputStream());   
  72. %>  
[注意]:
(1)contentType值设置为"image/JPEG"
 
3.result.jsp
 
 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  1. <html><body>  
  2. <%   
  3.     String input=request.getParameter("input");   
  4.     String code=(String)session.getAttribute("code");       
  5.     if(input.equals(code)){   
  6.         out.println("验证成功!");   
  7.     }else{   
  8.         out.println("验证失败!");   
  9.     }   
  10. %>  
  11. body>html>  

posted on 2010-09-01 16:39  ①块腹肌  阅读(13236)  评论(3编辑  收藏  举报

导航