1 使用Servlet实现验证码,涉及的知识点主要为java 绘图技术与session保存数据。
HTML页面
1 <html> 2 <image src='images/logo1.jpg' /><hr/> 3 <head><br/><title>登录</title> <br/><h1> 欢迎登录</h1></head> 4 <body> 5 <form action='/LoginValid/LoginVerify' method='post' > 6 用户id:<input type='text' name='userid' value=''> 7 用户密码:<input type='password' name='password' value=''> 9 验证码:<input type='text' name='inputCode' /> <img id='imgObj' src='/LoginValid/CreateCode2' /><a href='javascript:changeImg()' >换一张</a> 10 <input type='submit' value='登录' /> 11 </form> 12 </body>
13 <script type='text/javascript'> 14 function changeImg() {
var time = new Date();
document.getElementById('imgObj').src='/LoginValid/CreateCode2?d='+time;
} 15 </script> 16 </html>
生成验证码的工具类
1 public class CodeUtil { 2 3 //图片宽度 4 private static final int IMG_W=120; 5 //图片高度 6 private static final int IMG_H=30; 7 //验证码字符个数 8 private static final int NUM_CHS=5; 9 //验证码包含的具体字符 10 private static char[] chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890".toCharArray(); 11 private static Random rand = new Random(); 12 13 //图片x轴,y轴的位置基数 14 private static int codeX = 20; 15 private static int codeY = 25; 16 17 //字体大小 18 private static int fontH = 25; 19 20 public static Map<String, Object> generateImage() 21 { 22 BufferedImage image = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_RGB); 23 Graphics g = image.getGraphics(); 24 25 //设置图片背景色为白色 26 g.setColor(Color.WHITE); 27 g.fillRect(0, 0, IMG_W, IMG_H); 28 29 //设置字体大小 30 Font font = new Font("Fixedsys", Font.BOLD, fontH); 31 g.setFont(font); 32 33 //画边框 34 g.setColor(Color.BLACK); 35 g.drawRect(0, 0, IMG_W, IMG_H); 36 37 g.setColor(Color.BLACK); 38 //随机产生30条干扰线 39 for(int i=0; i<30; i++) 40 { 41 int x = rand.nextInt(IMG_W); 42 int y = rand.nextInt(IMG_H); 43 int xl = rand.nextInt(20); 44 int yl = rand.nextInt(20); 45 46 g.drawLine(x, y, xl, yl); 47 } 48 49 StringBuilder sb = new StringBuilder(); 50 for(int i=0; i<NUM_CHS; i++) 51 { 52 //从字符串中随机获取一个字符 53 String code = String.valueOf(chs[rand.nextInt(chs.length)]); 54 //随机设置画笔颜色 55 g.setColor(new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255))); 56 g.drawString(code, (i+1)*codeX, codeY); 57 sb.append(code); 58 } 59 60 Map<String, Object> map = new HashMap<String, Object>(); 61 map.put("code", sb.toString()); 62 map.put("codeImage", image); 63 return map; 64 65 } 66 67 }
后台产生验证码的Servlet
1 //禁止浏览器缓存随机图片 2 response.setDateHeader("Expires",-1); 3 response.setHeader("Cache-Control", "no-cache"); 4 response.setHeader("Pragma", "no-cache"); 5 6 //通知客户端以图片的方式打开发送过去的数据 7 response.setHeader("Content-Type", "image/jpeg"); 8 9 Map<String, Object> map = CodeUtil.generateImage(); 10 //将验证码保存至session 11 request.getSession().setAttribute("checkCode", map.get("code")); 12 ImageIO.write((RenderedImage)map.get("codeImage"), "jpg", response.getOutputStream());
后台验证用户输入的验证码与显示的是否一致的Servlet类
2 HttpSession session = request.getSession(); 3 String seCode = (String)session.getAttribute("checkCode"); 4 String inputCode = (String)request.getParameter("inputCode"); 5 if(seCode.equals(inputCode)) 6 { 7 request.getRequestDispatcher("/Main").forward(request, response); 8 } 9 else 10 { 11 request.getRequestDispatcher("/Err").forward(request, response); 12 }
博客参考:https://www.cnblogs.com/nanyangke-cjz/p/7049281.html