1,定义生成图片验证码的servlet,
package com.test.demo;
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.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class RandCode extends HttpServlet {
protected static Random random=new Random();
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("image/jpeg"); //设置输出类型为jpeg图片
int width=70;
int height=25; //验证图片的宽度,高度
Color back=getBack();
Color front=getFront(back);
String code=getString();
req.getSession().setAttribute("rand", code); //保存到session里面
BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g=bi.createGraphics(); //得到画布
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20)); //设置字体
g.setColor(back);
g.fillRect(0, 0, width, height); //画背景
g.setColor(front);
g.drawString(code,18,20); //画字符
for(int i=0,n=random.nextInt(20);i<n;i++){
g.fillRect(random.nextInt(width),random.nextInt(height),1,1);
} //产生至多20个噪点
ServletOutputStream so=resp.getOutputStream(); //得到二进制输出流
JPEGImageEncoder je=JPEGCodec.createJPEGEncoder(so); //对图片进行编码成jpeg格式
je.encode(bi);
so.flush(); //刷新缓存
}
/**
* @desc 得到图片背景色
* @author aj
* @date 2011-3-30
* */
protected static Color getBack(){
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}
/**
* @desc 生成颜色的反色
* @author aj
* @date 2011-3-30
* */
protected static Color getFront(Color c){
return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());
}
/**
* @desc 产生随机字符
* @author aj
* @date 2011-3-30
* */
protected static String getString(){
String old="23456789abcdefghijkmnpqrstuvwxyz"; //验证图片上面的随机字符
StringBuffer sb=new StringBuffer();
int j=0;
for(int i=0;i<4;i++){
j=random.nextInt(old.length());
sb.append(old.substring(j,j+1));
}
return sb.toString();
}
}
2,定义jsp文件,使用验证码并通过按钮切换验证码;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function change(){
document.getElementById("code").src="Rand?sc="+new Date().getTime();
};
</script>
</head>
<body>
<div>
<span><img src="Rand" id="code"/><button id="btn" style="margin-left:12px;" οnclick="change();">看不清</button></span>
</div>
</body>
</html>