处理的servlet如下:
/** * @author laker */ public class PicServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * 图片中出现的随机字符数组 */ private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; /** * 产生随机字符对象实例 */ public static Random random = new Random(); private String randomString = null; /** * 获取字符窜方法 * * @return 返回随机字符窜 */ public static String getRandomString() { StringBuffer sBuffer = new StringBuffer(); for (int i = 0; i < 6; i++) { sBuffer.append(CHARS[random.nextInt(CHARS.length)]); } return sBuffer.toString(); } /** * 获取随机的着色方法 * * @return 返回随机的背景着色 */ public static Color getRandomColor() { return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)); } /** * 获取对应的前景着色方法,以例识别 * * @return 获取对应的前景着色 */ public static Color getReverseColor(Color c) { return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue()); } /** * get方式调用 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * post方式调用 */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("更换图片" + request.getParameter("inputString")); // 响应设置为图片响应类型 response.setContentType("image/jpeg"); if (null != request.getParameter("inputString") && request.getParameter("inputString").toLowerCase() .equals(randomString.toLowerCase())) { System.out.println("验证码输入确"); //跳转到成功页面 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; response.sendRedirect(basePath + "success.jsp"); return; } else { System.out.println("验证码输入有误"); // 随机字符窜实例 randomString = getRandomString(); } // 获取当前session实例,没胡即创建之 request.getSession(true).setAttribute("randomString", randomString); // 定义图片长高 int width = 100; int height = 30; // 获取随机着色 Color color = getRandomColor(); // 获取对应着色 Color reverse = getReverseColor(color); // 创建图片 BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D gf = bi.createGraphics(); // 图片样式 gf.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16)); // 着色 gf.setColor(color); // 填充 gf.fillRect(0, 0, width, height); // 前景色 gf.setColor(reverse); // 绘制图片 gf.drawString(randomString, 18, 20); // 按高度随机绘制.出现的位置 for (int i = 0, n = random.nextInt(100); i < n; i++) { gf.drawRect(random.nextInt(width), random.nextInt(width), 1, 1); } // 输出流 ServletOutputStream out = response.getOutputStream(); // 以图片格试编码 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); // 刷新进行显示 out.flush(); } }
表单提交项:
<form id="frm" action="servlet/PicServlet"> <input type="text" name="inputString"> <img alt="验证码" src="servlet/PicServlet" id="picture" onload="btn.disabled=false;"> <input type="button" value="换一个" onclick="reloadImage()" id="btn"> <br> <input type="submit" value="提交"> </form>
JS:
function reloadImage() { document.getElementById('btn').disabled = true; document.getElementById('picture').src = 'servlet/PicServlet?ts=' + new Date() .getTime(); }