验证码HttpServlet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码HttpServlet相关的知识,希望对你有一定的参考价值。

  1 package servlet;
  2 import java.awt.Color;
  3 import java.awt.Font;
  4 import java.awt.Graphics;
  5 import java.awt.Graphics2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.IOException;
  8 import java.util.Random;
  9 import javax.imageio.ImageIO;
 10 import javax.servlet.ServletException;
 11 import javax.servlet.http.HttpServlet;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletResponse;
 14 /**
 15  * 通过javaAPI在内存中生成一张图片。
 16  * 通过response的输出流响应给客户端。
 17  */
 18 public class CheckCode extends HttpServlet {
 19     public void doGet(HttpServletRequest request, HttpServletResponse response)
 20             throws ServletException, IOException {
 21         // 禁止缓存
 22         // response.setHeader("Cache-Control", "no-cache");
 23         // response.setHeader("Pragma", "no-cache");
 24         // response.setDateHeader("Expires", -1);
 25         int width = 120;
 26         int height = 30;
 27         // 步骤一 绘制一张内存中图片
 28         BufferedImage bufferedImage = new BufferedImage(width, height,
 29                 BufferedImage.TYPE_INT_RGB);
 30         // 步骤二 图片绘制背景颜色 ---通过绘图对象
 31         Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
 32         // 绘制任何图形之前 都必须指定一个颜色
 33         graphics.setColor(getRandColor(200, 250));
 34         graphics.fillRect(0, 0, width, height);
 35         // 步骤三 绘制边框
 36         graphics.setColor(Color.WHITE);
 37         graphics.drawRect(0, 0, width - 1, height - 1);
 38         // 步骤四 四个随机数字
 39         Graphics2D graphics2d = (Graphics2D) graphics;
 40         // 设置输出字体
 41         graphics2d.setFont(new Font("宋体", Font.BOLD, 18));
 42         String words =
 43          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
 44         Random random = new Random();// 生成随机数
 45         // 将随机生成的验证码保存到session:
 46         StringBuffer buffer = new StringBuffer();
 47         // 定义x坐标
 48         int x = 10;
 49         for (int i = 0; i < 4; i++) {
 50             // 随机颜色
 51             graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random
 52                     .nextInt(110), 20 + random.nextInt(110)));
 53             // 旋转 -30 --- 30度
 54             int jiaodu = random.nextInt(60) - 30;
 55             // 换算弧度
 56             double theta = jiaodu * Math.PI / 180;
 57             // 生成一个随机数字
 58             int index = random.nextInt(words.length()); // 生成随机数 0 到 length - 1
 59             // 获得字母数字
 60             char c = words.charAt(index);
 61             // 将生成汉字 加入buffer
 62             buffer.append(c);
 63             // 将c 输出到图片
 64             graphics2d.rotate(theta, x, 20);
 65             graphics2d.drawString(String.valueOf(c), x, 20);
 66             graphics2d.rotate(-theta, x, 20);
 67             x += 30;
 68         }
 69         //Jis1 存储到Session
 70         request.getSession().setAttribute("code", buffer.toString());
 71         System.out.print("存储在Session中。");
 72         System.out.println(buffer.toString());
 73         // 步骤五 绘制干扰线
 74         graphics.setColor(getRandColor(160, 200));
 75         int x1;
 76         int x2;
 77         int y1;
 78         int y2;
 79         for (int i = 0; i < 30; i++) {
 80             x1 = random.nextInt(width);
 81             x2 = random.nextInt(12);
 82             y1 = random.nextInt(height);
 83             y2 = random.nextInt(12);
 84             graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
 85         }
 86         // 将上面图片输出到浏览器 ImageIO
 87         graphics.dispose();// 释放资源
 88         ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
 89     }
 90     public void doPost(HttpServletRequest request, HttpServletResponse response)
 91             throws ServletException, IOException {
 92         doGet(request, response);
 93     }
 94     /**
 95      * 取其某一范围的color
 96      * @param fc
 97      *            int 范围参数1
 98      * @param bc
 99      *            int 范围参数2
100      * @return Color
101      */
102     private Color getRandColor(int fc, int bc) {
103         // 取其随机颜色
104         Random random = new Random();
105         if (fc > 255) {
106             fc = 255;
107         }
108         if (bc > 255) {
109             bc = 255;
110         }
111         int r = fc + random.nextInt(bc - fc);
112         int g = fc + random.nextInt(bc - fc);
113         int b = fc + random.nextInt(bc - fc);
114         return new Color(r, g, b);
115     }
116 }

 

以上是关于验证码HttpServlet的主要内容,如果未能解决你的问题,请参考以下文章

验证码

实例:验证码

验证码画布生成以及点击图片切换验证码

网页登陆注册(jsp实现)验证码

java实现网页验证码

java验证码学习一