Servlet+JSP 实现验证码

来源:互联网 发布:dota2英雄知乎 编辑:程序博客网 时间:2024/06/05 10:30
  页面html代
  1. <body>  
  2.      验证码:<input type="text" name="checkcode"  id="checkcode"/>  
  3.     <img  src="<%=request.getContextPath() %>/imageServlet" alt="验证码" id="image" />  
  4.     <a href="javascript:reload();"><label>换一张</label></a><br>  
  5.     <input  type="button" value="提交"  onclick="javascript:verificationcode()">  
  6.     <span id="span"></span>  
  7. </body>  
这里图片的src是一个servlet,在下面会贴出来,为了便于使用异步后台验证,这里的提交按钮是button。调用verificationcode()方法。
  页面JavaScript代码
  1. <script type="text/javascript">  
  2.      function reload(){  
  3.         document.getElementById("image").src="<%=request.getContextPath() %>/imageServlet?date="+new Date().getTime();  
  4.         $("#checkcode").val("");   // 将验证码清空  
  5.     }   
  6.        
  7.      function verificationcode(){  
  8.          var text=$.trim($("#checkcode").val());  
  9.          $.post("${pageContext.request.contextPath}/verificationServlet",{op:text},function(data){  
  10.              data=parseInt($.trim(data));  
  11.              if(data>0){  
  12.                  $("#span").text("验证成功!").css("color","green");  
  13.              }else{  
  14.                  $("#span").text("验证失败!").css("color","red");  
  15.                  reload();  //验证失败后需要更换验证码  
  16.              }  
  17.          });  
  18.          $("#checkcode").val(""); // 将验证码清空  
  19.      }  
  20. </script>  
这里使用ImageServlet生成图片验证码,使用post后台验证,在点击标签换验证码图片的时候,一定要在调用生成图片前加一个时间戳,也就是上面的document.getElementById("image").src="<%=request.getContextPath() %>/imageServlet?date="+new Date().getTime();这句代码的最后加?后面的代码一定不能少,因为浏览器是由缓存的,如果不加时间戳,在点击标签换图片时,验证码图片不会正常更换。
ImageServlet生成验证码图片
  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.         request.setCharacterEncoding("utf-8");  
  3.           
  4.         BufferedImage bfi = new BufferedImage(80,25,BufferedImage.TYPE_INT_RGB);  
  5.         Graphics g = bfi.getGraphics();  
  6.         g.fillRect(008025);  
  7.   
  8.         //验证码字符范围  
  9.         char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();  
  10.         Random r = new Random();   
  11.         int index;    
  12.         StringBuffer sb = new StringBuffer(); //保存字符串  
  13.         for(int i=0; i<4; i++){  
  14.             index = r.nextInt(ch.length);  
  15.             g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));  
  16.             Font font = new Font("宋体"3020);  
  17.             g.setFont(font);  
  18.             g.drawString(ch[index]+"", (i*20)+223);  
  19.             sb.append(ch[index]);  
  20.         }  
  21.           
  22.         // 添加噪点  
  23.         int area = (int) (0.02 * 80 * 25);  
  24.         for(int i=0; i<area; ++i){  
  25.             int x = (int)(Math.random() * 80);  
  26.             int y = (int)(Math.random() * 25);  
  27.             bfi.setRGB(x, y, (int)(Math.random() * 255));  
  28.         }  
  29.           
  30.       //设置验证码中的干扰线  
  31.         for (int i = 0; i < 6; i++) {    
  32.               //随机获取干扰线的起点和终点  
  33.               int xstart = (int)(Math.random() * 80);  
  34.               int ystart = (int)(Math.random() * 25);  
  35.               int xend = (int)(Math.random() * 80);  
  36.               int yend = (int)(Math.random() * 25);  
  37.               g.setColor(interLine(1255));  
  38.               g.drawLine(xstart, ystart, xend, yend);  
  39.             }  
  40.         HttpSession session = request.getSession();  //保存到session  
  41.         session.setAttribute("verificationCode", sb.toString());  
  42.         ImageIO.write(bfi, "JPG", response.getOutputStream());  //写到输出流  
  43.     }  
为了便于比对结果,一定要将字符串保存到session。因为session是当前会话有效的,也就是说在你关闭浏览器之前,session里的值都是可以取到的。这里为了增加验证的难度,是不区分大小写的。噪点和干扰线的数量也是可调的。
随机产生干扰线颜色的方法:

  1. private static Color interLine(int Low, int High){  
  2.         if(Low > 255)  
  3.             Low = 255;  
  4.         if(High > 255)  
  5.             High = 255;  
  6.         if(Low < 0)  
  7.             Low = 0;  
  8.         if(High < 0)  
  9.             High = 0;  
  10.         int interval = High - Low;  
  11.         int r = Low + (int)(Math.random() * interval);  
  12.         int g = Low + (int)(Math.random() * interval);  
  13.         int b = Low + (int)(Math.random() * interval);  
  14.         return new Color(r, g, b);  
  15.       }  
最后是使用VerificationServlet对验证码图片上的字符与输入的字符串进行比对!把存入session中的verificationCode取出来与checkcode进行比对,返回结果。
VerificationServlet进行验证派生到我的代码片
  1. HttpSession session =request.getSession();  
  2. String verificationCode = (String)session.getAttribute("verificationCode");  
  3. String checkcode = request.getParameter("op");  
  4. PrintWriter out = response.getWriter();  
  5. if(checkcode.equals(verificationCode)){  
  6.     out.println(1);  
  7. }else{  
  8.     out.println(0);  
  9. }  
  10. out.flush();  
  11. out.close();  

实现效果:




0 0