1.生成验证码图片(Servlet)
importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.util.Random;importjavax.imageio.ImageIO;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class VerifyCode*/@WebServlet("/VerifyCode")public class VerifyCode extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicVerifyCode() {super();//TODO Auto-generated constructor stub
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
this.doPost(request, response);
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//首先设置浏览器不缓存
response.setHeader("cache-control", "no-cache");
response.setHeader("Expire", "-1"); //>0为缓存
response.setHeader("pragma", "no-cache");//存储正确的验证码
String vali_code = "";
String Vali_str= "abcdefghijklmnopqrstuvwsyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Color[] colors={ Color.BLACK, Color.RED, Color.BLUE };//1.创建图像
int width = 110, height = 40;int x_offset = 5, y_offset = 28;
BufferedImage img= newBufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);//2.创建画布
Graphics2D g =img.createGraphics();//3.为画布添加背景颜色为亮灰色
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);//fillRect是包括矩形框内部//4.为画布添加边框
g.setColor(Color.BLUE);
g.drawRect(0, 0, width - 1, height - 1);//drawRect是绘制矩形边框//5.绘制干扰线
for (int i = 0; i < 10; i++)
{
g.setColor(new Color(randNum(0, 255), randNum(0, 255), randNum(0, 255)));
g.drawLine(randNum(0, width), randNum(0, height), randNum(0, width), randNum(0, height));
}
g.setFont(new Font("黑体", Font.BOLD, 20));for (int i = 0; i < 4; i++)
{int r = randNum(-180, 180);
String code= Vali_str.charAt(randNum(0, Vali_str.length() - 1)) + "";
g.rotate(r/ 180 *Math.PI);
g.setColor(colors[randNum(0, colors.length)]);
g.drawString(code, x_offset+ i * 30, y_offset);
g.rotate(-r / 180 *Math.PI);
vali_code+=code;
}//将验证码的内容存储在HttpSession中
HttpSession session =request.getSession();
session.setAttribute("Valicode", vali_code);//通过response的输出流打给浏览器
ImageIO.write(img, "jpg", response.getOutputStream());
}private Random random = newRandom();//随机生成begin和end之间的一个int值
private int randNum(int begin, intend)
{return random.nextInt(end - begin) +begin;
}
}
2.JSP显示验证码
function changeValiImage(img){
img.src = "VerifyCode?time=" + new Date().getTime();//刷新
}
3.进行用户输入和正确验证码的比对
我在LoginServlet中
//1 获得用户输入的验证码
String verifyCode = request.getParameter("ValiImage");
//2 获得服务器session 存放数据 ,如果没有返回null
String sessionCacheData = (String) request.getSession().getAttribute("Valicode");
// *将服务器缓存session数据移除
request.getSession().removeAttribute("Valicode");
// ** 判断服务器是否存在
if(sessionCacheData == null){
request.setAttribute("message", "请不要重复提交");
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
//3 比较
if(! sessionCacheData.equalsIgnoreCase(verifyCode)){
//用户输入错误
// * 存放request作用域
request.setAttribute("message", "验证码输入错误");
// * 请求转发
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
//接登录方法...