生成验证码的Servlet,如图:
// Fig. 5.6_02: ImageCodeMakerServlet.java // 读取图像文件并生成验证码的Servlet package com.fatcat.webchart; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.awt.image.*; import java.awt.*; import javax.imageio.*; public class ImageCodeMakerServlet extends HttpServlet { String codeNumbers = ""; int width = 240, height = 60; // 处理 HTTP get 请求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 清空缓冲区 response.reset(); // 注意这里的MIME类型 response.setContentType("image/png"); // 设置页面不缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 创建一个 240X60 的图像 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 得到图形环境对象 g Graphics g = image.getGraphics(); // 填充背景 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); for (int i = 0; i < 4; i++) { drawCode(g, i, request); } // drawNoise(g, 30); // 绘制边框 g.setColor(Color.black); g.drawRect(0, 0, width - 1, height - 1); // 将验证码内容保存进session中 HttpSession session = request.getSession(true); session.setAttribute("codeNumbers", codeNumbers); // 重设字符串 codeNumbers = ""; // 利用ImageIO类的write方法对图像进行编码 ServletOutputStream sos = response.getOutputStream(); ImageIO.write(image, "PNG", sos); sos.close(); } // 加载图像文件并绘制验证码 public void drawCode(Graphics graphics, int i, HttpServletRequest request) { int number = (int)(Math.random() * 10); String imageFilePath =
request.getRealPath("\\images\\" + number + ".gif"; File imageFile = new File(imageFilePath); Image gifFile = null; try { gifFile = ImageIO.read(imageFile); } catch (Exception e) { System.out.println(e); } graphics.drawImage(gifFile, i* 60, 0, null); codeNumbers += number; } // 绘制干扰线 public void drawNoise(Graphics graphics, int lineNumber) { graphics.setColor(Color.YELLOW); for (int i = 0; i < lineNumber; i++) { int pointX1 = 1 + (int)(Math.random() * width); int pointY1 = 1 + (int)(Math.random() * height); int pointX2 = 1 + (int)(Math.random() * width); int pointY2 = 1 + (int)(Math.random() * height); graphics.drawLine(pointX1, pointY1, pointX2, pointY2); } } // 处理 HTTP post 请求, 和doGet一样 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } /************************************************************************** * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). * * All Rights Reserved. * * * * DISCLAIMER: The authors of this code have used their * * best efforts in preparing the code. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these codes. The authors * * shall not be liable in any event for incidental or consequential * * damages in connection with, or arising out of, the furnishing, * * performance, or use of these programs. * **************************************************************************/
↑返回目录
前一篇: servlet绘制甘特图
后一篇: servlet绘图入门:模拟投票统计的Servlet