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 ...
随机推荐
- NOR和NAND flash区别,RAM 和ROM区别
ROM是Read Only Memory的缩写.RAM是Random Access Memory的缩写.典型的RAM就是计算机的内存. RAM有两大类,一种称为静态RAM(Static RAM/SRA ...
- windows和linux下获取当前程序路径以及cpu数
#ifdef WIN32 #include <Windows.h> #else #include <stdio.h> #include <unistd.h> #en ...
- rem布局
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ajax框架,DWR介绍,应用,样例
使用Ajax框架 1. 简化JavaScript的开发难度 2. 解决浏览器的兼容性问题 3. 简化开发流程 经常使用Ajax框架 Prototype 一个纯粹的JavaScript函数库,对Ajax ...
- UITabBarController使用详解
UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod 程序等.UITabBarController通常作为整个程序的rootViewCo ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- Windows Server 2008 安装好之后的简单配置
1.禁用密码复杂度 在运行中输入GPEDIT.MSC 打开组策略,找到计算机配置->Windows设置->安全设置->账户策略中的密码策略,将“密码必须符合复杂性要求”设置为禁用即可 ...
- Fast InvSqrt()(平方根倒数速算法)
浮点数的平方根倒数常用于计算正规化矢量.3D图形程序需要使用正规化矢量来实现光照和投影效果,因此每秒都需要做上百万次平方根倒数运算,而在处理坐标转换与光源的专用硬件设备出现前,这些计算都由软件完成,计 ...
- js设置元素readonly属性注意事项
注意大小写,应该为:obj.readOnly = true;
- CSS超链接-光标-缩放
CSS超链接-光标-缩放 1.CSS 中链接的使用2.CSS 中光标的使用3.CSS 中缩放的使用 1.CSS 中链接的使用超链接伪类属性a:link 表示该超链接文字尚未被点选a:visite ...