package org.accp.ajax;
import java.awt.*;
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;
@SuppressWarnings("serial")
public class Action4 extends HttpServlet{
//定义随机生成的字符数组
char[] code = { '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' };
//创建随机数生成器
Random rand = new Random();
//定义获取指定范围内的随机颜色
Color getRandColor(int fc, int bc) {
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int n = Math.abs(fc - bc);
int r = fc + rand.nextInt(n);
int g = fc + rand.nextInt(n);
int b = fc + rand.nextInt(n);
return new Color(r, g, b);
}
Color getRandColor() {
return getRandColor(0, 255);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 设置页面,禁止图像缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
int width = 80; // 图像宽度
int height = 20; // 图像高度
int codeCount = 4; // 验证码长度
// 创建验证码图像image
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
// 设置验证码背景颜色
g.setColor(getRandColor(216, 250));
g.fillRect(0, 0, width, height);
// 设置验证码字体
g.setFont(new Font("Times New Roman", Font.PLAIN, height - 2));
// 设置验证码边框
//g.setColor(Color.BLACK);
//g.drawRect(0, 0, width - 1, height - 1);
// 随机生成160条干扰线
g.setColor(getRandColor(180, 200));
for (int i = 0; i < 160; i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
int xl = rand.nextInt(12);
int yl = rand.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 用一个randomCode来保存验证码
StringBuffer randomCode = new StringBuffer();
// 随机生成验证码
for (int i = 0; i < codeCount; i++) {
String randStr = String.valueOf(code[rand.nextInt(62)]);
g.setColor(getRandColor());
g.drawString(randStr, (rand.nextFloat() + 18) * i + 5, (rand
.nextFloat() + 3.5f) * 4);
randomCode.append(randStr); // 将随机产生的字符合成一个字符串
}
System.out.println(randomCode.toString());
// 将产生的验证码保存到session中
req.getSession().setAttribute("validatecode", randomCode.toString());
// 使图像生效
g.dispose();
// 将验证码图像输出到页面
ImageIO.write(image, "JPEG", resp.getOutputStream());
resp.getOutputStream().close();
}
}
/****************************************************************************************************/
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
验证码: