验证码实例
来源:互联网 发布:java培训课程体系 编辑:程序博客网 时间:2024/06/03 16:20
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 LiuLonglong19 * @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 @Override29 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), random50 .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/
0 0
- 验证码实例
- 客户端验证码实例
- 验证码实例
- 验证码实例源码
- java验证码实例
- 获取验证码实例
- PHP - 验证码实例
- PHP 验证码实例
- servlet验证码实例
- 图形验证码实例
- 验证码实例
- ThinkPHP 表单验证、验证码实例
- ASP.NET验证码实例
- 数字验证码测试实例
- php验证码代码实例
- Zend_Captcha生成验证码实例
- Session实例:一次性验证码
- Struts2 验证码图片实例
- Ext.js5(弹出搜索表单)(给一个面板添加数据)(34)
- 普天同庆
- Java算法---华为oj迷宫问题求解(深度优先搜索)
- iOS一些常用动画总结
- 编码以及计算机的发展
- 验证码实例
- MapReduce 算法设计-Local Aggregation
- QString和String相互转换
- Bootstrap快速学习笔记(2)表单系列之一
- Windows下Redis的安装使用
- 试题02 数字分类
- 第四十二篇:Adboost训练识别的经典的博文集(正负样本、积分图、分类器级联)
- 【算法】帽子问题
- 图的基本概念(-)