servlet生成随机验证码
package com.cgyue; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 生成图形验证码的Servlet
*/
public class ImageCodeMakerServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} /**
* @see javax.servlet.http.HttpServlet#void
* (javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 首先设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0); // 定义图片的宽度和高度
int width = 90, height = 40;
// 创建一个图像对象
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到图像的环境对象
Graphics g = image.createGraphics(); Random random = new Random();
// 用随机颜色填充图像背景
g.setColor(getRandColor(180, 250));
g.fillRect(0, 0, width, height);
for (int i = 0; i < 5; i++) {
g.setColor(getRandColor(50, 100));
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 4, 4);
}
// 设置字体,下面准备画随机数
g.setFont(new Font("", Font.PLAIN, 40));
// 随机数字符串
String sRand = "";
for (int i = 0; i < 4; i++) {
// 生成四个数字字符
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 生成随机颜色
g.setColor(new Color(20 + random.nextInt(80), 20 + random
.nextInt(100), 20 + random.nextInt(90)));
// 将随机数字画在图像上
g.drawString(rand, (17 + random.nextInt(3)) * i + 8, 34); // 生成干扰线
for (int k = 0; k < 12; k++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(9);
int yl = random.nextInt(9);
g.drawLine(x, y, x + xl, y + yl);
}
} // 将生成的随机数字字符串写入Session
request.getSession().setAttribute("randcode", sRand);
// 使图像生效
g.dispose();
// 输出图像到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
} /**
* 产生一个随机的颜色
* @param fc 颜色分量最小值
* @param bc 颜色分量最大值
* @return
*/
public Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255){
fc = 255;
}
if (bc > 255){
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
servlet生成随机验证码的更多相关文章
- java Servlet生成随机验证码
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
随机推荐
- BadgeView新提示开源工具类
BadgeView是使用某个图标作为新功能的提醒,类似于收到短息后短信图标的右上方有信息数目或者其他的显示性提示.BadgeView很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 自我总结
要熟练掌握比较器Comparator public final Comparator<T> MY_COMPARATOR = new myComparator(); //定义比较器 .... ...
- 剖析magento中关于Email模板的设置
public function send() { $emailTemplate = Mage::getModel('core/email_template'); // ...
- Magento - get Attribute Options of the dropdown type attribute
$attribute_code = "color"; $attribute_details = Mage::getSingleton("eav/config" ...
- 机房收费系统之vb报表的模板的制作(一)
机房收费系统有报表的功能,报表对于我们来说有点陌生.这不是会计的事吗?怎么机房收费系统也參合进来了,事实上我们学会了报表的步骤.理解了代码后.报表变得不是非常难,世上无难事,仅仅怕肯登攀 ...
- android入门——BroadCast(2)
自定义广播 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&q ...
- IOS app启动过程
1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 3.delegate对象开始处理(监 ...
- tmux 命令
创建并指定session名字tmux new -s $session_name 删除sessionCtrl+b :kill-session 临时退出sessionCtrl+b d 列出sessiont ...
- PHP性能如何实现全面优化?
性能是网站运行是否良好的关键因素, 网站的性能与效率影响着公司的运营成本及长远发展,编写出高质高效的代码是我们每个开发人员必备的素质,也是我们良好的职业素养. 如何优化PHP性能呢? 一.变量(重要) ...
- Matlab图像直方图相关函数
图像的灰度直方图(H是图像a.bmp的数据矩阵) imhist(H):%显示a的直方图 histeq(H); %将图像a进行直方图均衡化 adapthisteq(H); %将图像a进行直方图均衡化 i ...