servlet编写验证码,关于验证码生成的代码

servlet编写验证码,servlet验证码

出于供给就写了二个生成验证码的servlet 简洁实用

图片 1

 1 import java.awt.Color;
 2 import java.awt.Font;
 3 import java.awt.Graphics;
 4 import java.awt.image.BufferedImage;
 5 import java.io.IOException;
 6 import java.util.Date;
 7 import java.util.Random;
 8 import javax.imageio.ImageIO;
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 /**
16  * Servlet implementation class ValidateCode
17  */
18 @WebServlet("/ValidateCode")
19 public class ValidateCode extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public ValidateCode() {
26         super();
27         // TODO Auto-generated constructor stub
28     }
29 
30     /**
31      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32      */
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         
35         // 设置页面不缓存
36           response.setHeader("Pragma", "No-cache");
37           response.setHeader("Cache-Control", "no-cache");
38           response.setDateHeader("Expires", 0);
39           // 设置图片的长宽
40           int width = 62, height = 22;
41           // ////// 创建内存图像
42           BufferedImage image = new BufferedImage(width, height,
43             BufferedImage.TYPE_INT_RGB);
44           // 获取图形上下文
45           Graphics g = image.createGraphics();
46           // 设定图像背景色(因为是做背景,所以偏淡)
47           g.setColor(getRandColor(180, 250));
48           g.fillRect(0, 0, width, height);
49           // 设置字体
50           g.setFont(new Font("Times New Roman", Font.PLAIN, 17));
51           // /////设置默认生成4个验证码
52           int length = 4;
53           java.util.Random rand = new Random(); // 设置随机种子
54           /*
55            * if (request.getParameter("length") != null) { try { length =
56            * Integer.parseInt(request.getParameter("length")); }catch
57            * (NumberFormatException e) {} }
58            */
59           // 设置备选验证码:包括"a-z"和数字"0-9"
60           String base = "abcdefghijklmnopqrstuvwxyz0123456789";
61           int size = base.length();
62           StringBuffer str = new StringBuffer();
63           for (int i = 0; i < length; i++) {
64            int start = rand.nextInt(size);
65            String tmpStr = base.substring(start, start + 1);
66            str.append(tmpStr);
67            // 生成随机颜色(因为是做前景,所以偏深)
68            g.setColor(getRandColor(10, 150));
69            // 将此字画到图片上
70            // g.drawString(str.toString(), 4, 17);
71            g.drawString(tmpStr, 13 * i + 6 + rand.nextInt(5), 14 + rand
72              .nextInt(6));
73           }
74           // 将认证码存入session
75           request.getSession().setAttribute("valiCode", str.toString());
76           // 图象生效
77           g.dispose();
78           // 输出图象到页面
79           ImageIO.write(image, "JPEG", response.getOutputStream());
80          }
81          public void doPost(HttpServletRequest request, HttpServletResponse response)
82            throws ServletException, IOException {
83           this.doGet(request, response);
84          }
85          // 给定范围获得一个随机颜色
86          Color getRandColor(int fc, int bc) {
87           Random random = new Random();
88           if (fc > 255)
89            fc = 255;
90           if (bc > 255)
91            bc = 255;
92           int r = fc + random.nextInt(bc - fc);
93           int g = fc + random.nextInt(bc - fc);
94           int b = fc + random.nextInt(bc - fc);
95           return new Color(r, g, b);
96          }
97         }

View Code

 

由于需求就写了二个生成验证码的servlet 简洁实用 1 import java.awt.Color;
2 import java.awt.Font; 3 import java.awt.Graph…

java_web 关于验证码生成的代码,java_web验证码生成

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

 

关于验证码生成的代码,java_web验证码生成 1 package com.itheima.servlet;
2 3 import java.awt.Color; 4 import java.awt.Font; 5 import
java.awt.Graphics; 6 im…

发表评论

电子邮件地址不会被公开。 必填项已用*标注