}
268
}
在servlet里获取验证码
1 package com.lz.Servlet;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
6 import javax.imageio.ImageIO;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import com.lz.Tools.VCodeGenerator;
16 /**
17 * 获取验证码
18 * @author bojiangzhou
19 *
20 */
21 public class GetVCodeServlet extends HttpServlet {
23 private static final long serialVersionUID = 3259532010990625726L;
25 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26 //验证码不能缓存
27 response.setHeader("Expires", "-1");
28 response.setHeader("cache-control", "no-cahce");
29 response.setHeader("pragma", "no-cache");
31 VCodeGenerator vcg = new VCodeGenerator();
32 //取得验证码
33 String vcode = vcg.generatorVCode();
34 //获取验证码图片
35 // BufferedImage vcodeImage = vcg.generatorRotateVCodeImage(vcode, true);
36 BufferedImage vcodeImage = vcg.generatorVCodeImage(vcode, true);
37 //将验证码保存到session域对象
38 request.getSession().setAttribute("vcode", vcode);
39 //输出验证码图片
40 ImageIO.write(vcodeImage, "gif", response.getOutputStream());
41 }
前台在img标签里用src获取验证码图片即可
<img class="vcode" src="GetVCodeServlet" />
OK!!!
下载源文件
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。