首页 > 代码库 > jsp生成验证码

jsp生成验证码

  1 package servlet;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Graphics;
  6 import java.awt.image.BufferedImage;
  7 import java.io.IOException;
  8 import java.util.Random;
  9 
 10 import javax.imageio.ImageIO;
 11 import javax.servlet.ServletException;
 12 import javax.servlet.annotation.WebServlet;
 13 import javax.servlet.http.HttpServlet;
 14 import javax.servlet.http.HttpServletRequest;
 15 import javax.servlet.http.HttpServletResponse;
 16 
 17 @SuppressWarnings("serial")
 18 @WebServlet(urlPatterns = "/CreateCodeServlet")
 19 public class CreateCodeServlet extends HttpServlet {
 20 
 21     private char[] unit = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
 22     private Random random = new Random();
 23     StringBuilder sb = new StringBuilder();
 24 
 25     @Override
 26     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 27             throws ServletException, IOException {
 28         this.createCode(req, resp);
 29         outputImage(req, resp);
 30     }
 31 
 32     /**
 33      * 生成验证码图片并输出到页面
 34      * 
 35      * @param req
 36      * @param resp
 37      * @throws IOException
 38      */
 39     private void outputImage(HttpServletRequest req, HttpServletResponse resp)
 40             throws IOException {
 41         // 设置返回JPEG
 42         resp.setContentType("image/jpeg");
 43         // 设置页面不缓存
 44         resp.setHeader("Pragma", "No-cache");
 45         resp.setHeader("Cache-Control", "no-cache");
 46         resp.setDateHeader("Expires", 0);
 47         
 48         // 在内存中创建图象
 49         int width = 60;
 50         int height = 20;
 51         BufferedImage image = new BufferedImage(width, height,
 52                 BufferedImage.TYPE_INT_RGB);
 53 
 54         Graphics g = image.getGraphics();
 55         // 设定背景色
 56         g.setColor(getRandomColor());
 57         g.fillRect(0, 0, width, height);
 58         // 设定字体
 59         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
 60         // 画边框
 61         g.setColor(getRandomColor());
 62         g.drawRect(0, 0, width - 1, height - 1);
 63         // 随机产生100条干扰线,使图象中的认证码不易被其它程序探测到
 64         g.setColor(getRandomColor());
 65         for (int i = 0; i < 10; i++) {
 66             int x = random.nextInt(width);
 67             int y = random.nextInt(height);
 68             int xl = random.nextInt(12);
 69             int yl = random.nextInt(12);
 70             g.drawLine(x, y, x + xl, y + yl);
 71         }
 72         g.setColor(getRandomColor());
 73         g.drawString(req.getSession().getAttribute("code").toString(), 10, 15);
 74 
 75         // 把图片写到img标签
 76         ImageIO.write(image, "JPEG", resp.getOutputStream());
 77     }
 78 
 79     /**
 80      * 返回随机颜色
 81      * 
 82      * @return 随机颜色
 83      */
 84     private Color getRandomColor() {
 85         int r = random.nextInt(255);
 86         int g = random.nextInt(255);
 87         int b = random.nextInt(255);
 88         return new Color(r, g, b);
 89     }
 90 
 91     /**
 92      * 创建验证码并且放到session中
 93      * @param req
 94      * @param resp
 95      */
 96     private void createCode(HttpServletRequest req, HttpServletResponse resp) {
 97         sb.delete(0, sb.length());
 98         for (int i = 0; i < 4; i++) {
 99             int index = random.nextInt(36);
100             sb.append(unit[index]);
101         }
102         req.getSession().setAttribute("code", sb.toString());
103     }
104 }