package action.ImageValidator;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
// 验证码图片的宽度。
private int width = 60;
// 验证码图片的高度。
private int height = 20;
// 验证码字符个数
private int codeCount = 4;
private int x = 0;
// 字体高度
private int fontHeight;
private int codeY;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
@Override
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
doPost(arg0, arg1);
}
public void init() throws ServletException {
// 从web.xml中获取初始信息
// 宽度
String strWidth = this.getInitParameter("width");
// 高度
String strHeight = this.getInitParameter("height");
// 字符个数
String strCodeCount = this.getInitParameter("codeCount");
// System.out.println("strWidth==" + strWidth + ", strHeight==" + strHeight
// + ", strCodeCount==" + strCodeCount);
// 将配置的信息转换成数值
try {
if (strWidth != null && strWidth.length() != 0) {
width = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
height = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
}
x = width / (codeCount + 1);
fontHeight = height;
codeY = height;
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
g.setColor(getRandColor(220, 250));
g.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Arial Bold", Font.PLAIN, fontHeight-5);
// 设置字体。
g.setFont(font);
// 画边框。
// g.setColor(Color.pink);
// g.drawRect(0, 0, width - 1, height - 1);
// 随机产生150条干扰线,使图象中的认证码不易被其它程序探测到。
/*
g.setColor(getRandColor(120, 200));
for (int i = 0; i < 150; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(10);
int yl = random.nextInt(10);
g.drawLine(x, y,x+xl,y+yl);
}
*/
// 随机产生450个干扰点,使图象中的认证码不易被其它程序探测到。
g.setColor(getRandColor(120,200));
for(int i=0;i<550;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
g.drawOval(x,y,0,0);
}
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(62)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
g.setColor(getRandColor(20, 130));
// 用随机产生的颜色将验证码绘制到图像中。
g.drawString(strRand, (i + 1) * x - 7, codeY - 5);
// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
/**
* 产生随机颜色
*
* @param num1
* @param num2
* @return Color
*/
public static Color getRandColor(int num1, int num2) {
Random random = new Random();
if (num1 > 255)
num1 = 255;
if (num2 > 255)
num2 = 255;
int r = num1 + random.nextInt(num2 - num1);
int g = num1 + random.nextInt(num2 - num1);
int b = num1 + random.nextInt(num2 - num1);
return new Color(r, g, b);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
验证码+jquery+无刷新验证
共13个文件
js:3个
class:2个
java:2个
5星 · 超过95%的资源 需积分: 17 188 下载量 190 浏览量
2012-04-08
16:52:04
上传
评论 2
收藏 39KB RAR 举报
温馨提示
用java+servlet+jquery完成的一个验证码,无刷新验证功能,简单,干净,不错.从网上淘来的~
资源推荐
资源详情
资源评论
收起资源包目录
ImageValidator.rar (13个子文件)
ImageValidator
src
action
ImageValidator
ResultServlet.java 1KB
ImageServlet.java 5KB
.classpath 362B
.mymetadata 315B
.myeclipse
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
js
changeImg.js 566B
jquery-1.2.6.js 98KB
valiCode.js 2KB
ImageValidator.jsp 905B
WEB-INF
classes
action
ImageValidator
ImageServlet.class 5KB
ResultServlet.class 2KB
lib
web.xml 1KB
共 13 条
- 1
xingyunpi
- 粉丝: 45
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页