验证码实例
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());
}
实现效果:
![](http://images0.cnblogs.com/blog2015/569715/201507/211310228163437.png)
如果部分浏览器显示为一堆乱码时,请加上以下代码来设置浏览器接受的形式:
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
【来源】http://www.cnblogs.com/mvilplss/
相关文章推荐
- iOS一些常用动画总结
- 编码以及计算机的发展
- MapReduce 算法设计-Local Aggregation
- 数据库的测试
- Mysql 性能查询命令
- 航模项目流程 1——整体规划、确定软硬件、研讨 Schedule
- MySQL体系架构
- 先芯科技举办第三届篮球赛
- Java算法---华为oj迷宫问题求解(深度优先搜索)
- 关于读者来信的一些思考
- Ext.js5(弹出搜索表单)(给一个面板添加数据)(34)
- CSS 最核心的四个概念
- 普天同庆
- classpath、path、JAVA_HOME的作用及JAVA环境变量配置
- classpath、path、JAVA_HOME的作用及JAVA环境变量配置
- 【学习总结】【多线程】一些面试题
- Redis3.0.5配置文件详解
- NS2学习路程---1NS仿真创建网络基本框架
- Why The Hell Would I Use Node.js? A Case-by-Case Tutorial
- 微软与网安合资背后:警惕中国 ICT 产业的变和博弈?