由于需要就写了一个生成验证码的servlet 简洁实用
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.Graphics; 4 import java.awt.image.BufferedImage; 5 import java.io.IOException; 6 import java.util.Date; 7 import java.util.Random; 8 import javax.imageio.ImageIO; 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 /** 16 * Servlet implementation class ValidateCode 17 */ 18 @WebServlet("/ValidateCode") 19 public class ValidateCode extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public ValidateCode() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 35 // 设置页面不缓存 36 response.setHeader("Pragma", "No-cache"); 37 response.setHeader("Cache-Control", "no-cache"); 38 response.setDateHeader("Expires", 0); 39 // 设置图片的长宽 40 int width = 62, height = 22; 41 // // 创建内存图像 42 BufferedImage image = new BufferedImage(width, height, 43 BufferedImage.TYPE_INT_RGB); 44 // 获取图形上下文 45 Graphics g = image.createGraphics(); 46 // 设定图像背景色(因为是做背景,所以偏淡) 47 g.setColor(getRandColor(180, 250)); 48 g.fillRect(0, 0, width, height); 49 // 设置字体 50 g.setFont(new Font("Times New Roman", Font.PLAIN, 17)); 51 // /设置默认生成4个验证码 52 int length = 4; 53 java.util.Random rand = new Random(); // 设置随机种子 54 /* 55 * if (request.getParameter("length") != null) { try { length = 56 * Integer.parseInt(request.getParameter("length")); }catch 57 * (NumberFormatException e) {} } 58 */ 59 // 设置备选验证码:包括"a-z"和数字"0-9" 60 String base = "abcdefghijklmnopqrstuvwxyz0123456789"; 61 int size = base.length(); 62 StringBuffer str = new StringBuffer(); 63 for (int i = 0; i < length; i++) { 64 int start = rand.nextInt(size); 65 String tmpStr = base.substring(start, start + 1); 66 str.append(tmpStr); 67 // 生成随机颜色(因为是做前景,所以偏深) 68 g.setColor(getRandColor(10, 150)); 69 // 将此字画到图片上 70 // g.drawString(str.toString(), 4, 17); 71 g.drawString(tmpStr, 13 * i + 6 + rand.nextInt(5), 14 + rand 72 .nextInt(6)); 73 } 74 // 将认证码存入session 75 request.getSession().setAttribute("valiCode", str.toString()); 76 // 图象生效 77 g.dispose(); 78 // 输出图象到页面 79 ImageIO.write(image, "JPEG", response.getOutputStream()); 80 } 81 public void doPost(HttpServletRequest request, HttpServletResponse response) 82 throws ServletException, IOException { 83 this.doGet(request, response); 84 } 85 // 给定范围获得一个随机颜色 86 Color getRandColor(int fc, int bc) { 87 Random random = new Random(); 88 if (fc > 255) 89 fc = 255; 90 if (bc > 255) 91 bc = 255; 92 int r = fc + random.nextInt(bc - fc); 93 int g = fc + random.nextInt(bc - fc); 94 int b = fc + random.nextInt(bc - fc); 95 return new Color(r, g, b); 96 } 97 }