一、调用验证码
<img src='/Web应用名/CreateCode'/>
二、生成验证码
public class CreateCode extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
// 7.禁止浏览器缓存随机图片
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
//6.通知客户机以图片方式打开发送过去的数据
response.setHeader("Content-Type", "image/jpeg");
//1.在内存中创建一副图片
BufferedImageimage = new BufferedImage(60, 30,
BufferedImage.TYPE_INT_RGB);
//2.向图片上写数据
Graphics g = image.getGraphics();
// 设背景色
g.setColor(Color.BLACK);
g.fillRect(0,0, 60, 30);
//3.设置写入数据的颜色和字体
g.setColor(Color.RED);
g.setFont(new Font(null,Font.BOLD, 20));
//4.向图片上写数据
String num =makeNum();
//这句话就是把随机生成的数值,保存到session
request.getSession().setAttribute("checkcode",num); 通过session就可以直接去到随即生成的验证码了
g.drawString(num,0, 20);
//5.把写好数据的图片输出给浏览器
ImageIO.write(image,"jpg",response.getOutputStream());
}
//该函数时随机生成7位数字
publicString makeNum() {
Random r = newRandom();
//9999999可以生成7位
String num =r.nextInt(9999) + "";
StringBuffersb = new StringBuffer();
//如果不够4位,前面补零
for (int i =0; i < 4 - num.length(); i++) {
sb.append("0");
}
num =sb.toString() + num;
return num;
}