1
//1- 简单的验证码实现步骤
package com.hao.demo01;
2
3 /*
4 * 向浏览器发送图片
5 */
6 import java.awt.Color;
7 import java.awt.Font;
8 import java.awt.Graphics;
9 import java.awt.image.BufferedImage;
10 import java.io.IOException;
11 import java.io.OutputStream;
12
13 import javax.imageio.ImageIO;
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 public class ImageServlet extends HttpServlet{
20 @Override
21 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
22 throws ServletException, IOException {
23 //1- 获得参数的值
24 String param = req.getParameter("param");
25
26 if(param == null){
27 param = "11111111";
28 }
29 //2- 在内存中创建图片
30 BufferedImage image = new BufferedImage(200, 20, BufferedImage.TYPE_INT_RGB);
31 //2.1-获得该图片的画笔对象
32 Graphics g = image.getGraphics();
33 //设置画笔的颜色黑色
34 g.setColor(Color.BLACK);
35 //画一个黑色矩形框
36 g.fillRect(0, 0, 200, 20);
37 //重新设置画笔颜色
38 g.setColor(Color.WHITE);
39 //设置画笔的字体格式
40 g.setFont(new Font("Calibri", Font.BOLD, 36));
41 //将字符画到画布上
42 g.drawString(param, 20, 10);
43 g.dispose();//释放画笔对象
44 //3-设置MIME类型
45 resp.setContentType("image/jpeg");
46 OutputStream os = resp.getOutputStream();
47 //将服务器中内存里面的图片,发送给浏览器
48 ImageIO.write(image, "JPEG", os);
49 os.close();
50 }
51 }
//2 - 实现用户登录的验证码小程序
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Insert title here</title>
6 </head>
7 <body>
8 <h1>用户登录</h1>
9 <form action="">
10 验证码:<input type="text" name="validate" size=20/>
11 <img src="/webday07/verify" id="im" width="80" height="25"
12 onClick="this.src=this.src+'?'+Math.random()">
13 <a href="javascript:im.onclick()">看不清,换一个</a>
14 </form>
15 </body>
16 </html>
1
//实现验证码的类
package com.hao.demo02;
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.io.OutputStream;
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
17 public class VerifyCodeServlet extends HttpServlet{
18 //得到验证码的字符集对象
19 String[] codes = SourceTool.getCodeLib();
20 Random random = new Random();
21 int width = 80;
22 int height = 25;
23
24 @Override
25 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
26 throws ServletException, IOException {
27 //设置正文MIME类型
28 resp.setContentType("image/jpeg");
29 //输出流对象
30 OutputStream out = resp.getOutputStream();
31 //在内存中创建图片
32 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
33 //获得画笔
34 Graphics g = image.getGraphics();
35 //设置画笔的颜色
36 g.setColor(new Color(200, 200, 200));
37 //绘制矩形
38 g.fillRect(0, 0, width, height);
39 //绘制干扰线
40 //重新设置画笔颜色
41 g.setColor(new Color(150, 150, 150));
42
43 for(int i = 0; i < 20; i++){
44 int x1 = random.nextInt(width);
45 int y1 = random.nextInt(height);
46 int x2 = random.nextInt(width);
47 int y2 = random.nextInt(height);
48 g.drawLine(x1, y1, x2, y2);
49 }
50
51 //保存随机生成的四个验证码
52 StringBuilder sb = new StringBuilder();
53 //生成证码并绘制到图片上
54 //四个验证码
55 g.setFont(new Font("Calibri", Font.BOLD, 20));
56 for(int i = 0; i < 4; i++){
57 String code = codes[random.nextInt(62)];
58 sb.append(code);
59 g.setColor(new Color(random.nextInt(150), random.nextInt(150), random.nextInt(150)));
60 g.drawString(code, i*16 + 15, 20);
61 }
62
63 g.dispose();
64
65 //将服务器端的图片发送给客户端
66 ImageIO.write(image, "JPEG", out);
67 out.close();
68 //将验证码保存在回话中.
69 }
70 }
1
//获取字母和数字的工具类
package com.hao.demo02;
2
3 public class SourceTool {
4 //静态方法,用于保存随机的字符序列
5 public static String[] getCodeLib(){
6 String[] verifyCodes = new String[62];
7 //将数字,小写字母,大写字母填充到数组中
8 //将0~9保存到数组中
9 for(int i = 0; i < 10; i++){
10 verifyCodes[i] = String.valueOf(i);
11 }
12 //将小写字母a~z保存到数组中
13 for(int i = 0; i < 26; i++){
14 verifyCodes[i + 10] = String.valueOf((char)(97 + i));
15 }
16 //将大写字母A~Z保存到数组中
17 for(int i = 0; i < 26; i++){
18 verifyCodes[i + 36] = String.valueOf((char)(65 + i));
19 }
20 return verifyCodes;
21 }
22 }