servlet向浏览器输出验证码图片,注册页面的<img> 标签的src属性只需要指向该servlet即可
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通知浏览器不要缓存图片
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
//通知浏览器以图片方式打开
response.setHeader("Content-type", "image/jpeg");
//在内存中创建一张图片
BufferedImage image = new BufferedImage(100, 20, BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D g = (Graphics2D) image.getGraphics();
<span style="white-space:pre"> </span>//设置背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, 100, 20);
//向图片上写数据
g.setColor(Color.GRAY);
g.setFont(new Font(null,Font.BOLD,20));
g.drawString(makeNum(), 0, 20);
//写到浏览器
ImageIO.w