您的位置:首页 > 其它

验证码实例

2015-12-21 09:19 197 查看
package com.lljr.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.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
16 /**
17  * 验证码
18  * @AUTHER LiuLonglong
19  * @Motto Goals determine what you are going to be.
20  * @URL http://www.cnblogs.com/mvilplss/ 21  */
22 public class VerificationServlet extends HttpServlet {
23
24     private static final long serialVersionUID = 1L;
25     // 设置字母的大小,大小
26     private Font mFont = new Font("宋体", Font.BOLD, 28);
27
28     @Override
29     protected void service(HttpServletRequest request,
30             HttpServletResponse response) throws ServletException, IOException {
31         // 创建画板缓冲区
32         int width = 132;
33         int height = 45;
34         BufferedImage img = new BufferedImage(width, height,
35                 BufferedImage.TYPE_INT_RGB);
36         // 获取画板
37         Graphics g = img.getGraphics();
38         g.setColor(new Color(255, 255, 255));
39         g.fillRect(0, 0, width, height);// 填充颜色
40         g.setColor(new Color(102, 102, 102));
41         g.drawRect(0, 0, width - 1, height - 1);// 画出边框
42         // 画随机线
43         Random random = new Random();
44         for (int i = 0; i < 15; i++) {
45             int x = random.nextInt(width - 1);
46             int y = random.nextInt(height - 1);
47             int xl = random.nextInt(12) + 1;
48             int yl = random.nextInt(48) + 1;
49             g.setColor(new Color(random.nextInt(255), random.nextInt(5), random
50                     .nextInt(10)));
51             g.drawLine(x, y, x + xl, y + yl);
52         }
53         // 画上字符串
54         g.setColor(new Color(25, 11, 222));
55         g.setFont(mFont);
56         String[] chars = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
57                 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
58                 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "s",
59                 "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
60                 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
61                 "W", "S", "Y", "Z" };
62         String randomString = "";
63         for (int i = 0; i < 6; i++) {
64             int index = random.nextInt(62);
65             int rc = random.nextInt(256);
66             int gc = random.nextInt(256);
67             int bc = random.nextInt(25);
68             g.setColor(new Color(rc, gc, bc));
69             g.drawString(chars[index], 21 * i + 5, 30);
70             randomString += chars[index];
71         }
72         g.dispose();
73         g.dispose();
74         HttpSession session = request.getSession();
75         session.setAttribute("system_verification_code", randomString);
76         ImageIO.write(img, "JPEG", response.getOutputStream());
77     }
78
79 }


页面请求验证码:

<img id="_verification" src="${basePath}/verificationServlet.servlet"></img>

<a href="javascript:void(0);" onclick="_verification_reload()">刷新</a>

刷新按钮js:

function _verification_reload(){

$("#_verification").attr("src","${basePath}/verificationServlet.servlet?a="+Math.random());

}

实现效果:



如果部分浏览器显示为一堆乱码时,请加上以下代码来设置浏览器接受的形式:

response.setHeader("Pragma", "No-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

response.setContentType("image/jpeg");

【来源】http://www.cnblogs.com/mvilplss/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: