JAVAWEB (三) 登陆页面验证码的实现

    在进行登陆的时候,往往会需要输入验证码,那么验证码是怎么实现的呢?

    首先我们的验证码图片是在JSP页面中展示的,所以先从JSP页面入手。

    流程:登录界面->后台生成图片和这个图片对应的数字(把数字存入session->前台的jsp页面获得图片所对应的数字->当用户点击提交后判断用户输入的与存储的数字是否一样。

  1. 登录主页面(login.jsp
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>登录</title>  
  9. <script type="text/javascript">  
  10.     function refresh() {  
  11.         //IE存在缓存,需要new Date()实现更换路径的作用  
  12.         document.getElementById("image").src="../login/image.jsp?"+new Date();  
  13.     }  
  14. </script>  
  15. </head>  
  16. <body>  
  17.     <form action="../login/check.jsp" method="post" id="loginForm" name="loginForm">  
  18.         用户名:<input type="text" name="userName">  
  19.         密码:<input type="password" name="userPwd">  
  20.         验证码:<input type="text" name="code" maxlength="4">  
  21.         <img id="image" border="0"  onclick="refresh()" src="../login/image.jsp" title="点击更换图片">  
  22.         <input type="submit" value="登录">  
  23.     </form>  
  24. </body>  
  25. </html>  

     2. 验证码生成jsp(image.jsp)

  1. <%@page import="javax.imageio.ImageIO"%>  
  2. <%@page import="java.awt.Font"%>  
  3. <%@page import="java.awt.Graphics"%>  
  4. <%@page import="java.awt.image.BufferedImage"%>  
  5. <%@page import="java.util.Random"%>  
  6. <%@page import="java.awt.Color"%>  
  7. <%@ page language="java" contentType="image/JPEG; charset=UTF-8"  
  8.     pageEncoding="UTF-8"%>  
  9. <%!  
  10.     //获取随机颜色  
  11.     Color getRandColor(int fc,int bc){  
  12.     Random random = new Random();  
  13.     if(fc>255) fc=255;  
  14.     if(bc>255) bc=255;  
  15.     int r = fc + random.nextInt(bc - fc);  
  16.     int g = fc + random.nextInt(bc - fc);  
  17.     int b = fc + random.nextInt(bc - fc);  
  18.     return new Color(r,g,b);  
  19.     }  
  20. %>  
  21. <%  
  22.     //设置页面不缓存   
  23.     response.setHeader("Pragma""No-cache");  
  24.     response.setHeader("Cache-Control""no-cache");     
  25.     response.setDateHeader("Expires"0);  
  26.     //在内存中创建图像  
  27.     int width = 60;  
  28.     int height = 20;  
  29.     BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
  30.     //获取图形上下文  
  31.     Graphics g = image.getGraphics();  
  32.     //随机类  
  33.     Random random = new Random();  
  34.     //设定背景  
  35.     g.setColor(getRandColor(200250));  
  36.     g.fillRect(00, width, height);  
  37.     //设定字体  
  38.     g.setFont(new Font("Times New Roman",Font.PLAIN,18));  
  39.    //随机产生干扰线  
  40.    g.setColor(getRandColor(160200));     
  41.    for (int i = 0; i < 100; i++) {     
  42.         int x = random.nextInt(width);     
  43.         int y = random.nextInt(height);     
  44.         int xl = random.nextInt(12);     
  45.         int yl = random.nextInt(12);     
  46.         g.drawLine(x, y, x + xl, y + yl);     
  47.    }   
  48.    //随机产生4位验证码  
  49.    String[] codes = {"2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};  
  50.    String code = "";  
  51.    for(int i=0;i<4;i++){  
  52.        String str = codes[random.nextInt(codes.length)];  
  53.        code += str;  
  54.        // 将认证码显示到图象中  
  55.        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));  
  56.        //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成     
  57.        g.drawString(str, 13 * i + 616);     
  58.    }  
  59.     // 将认证码存入SESSION     
  60.    session.setAttribute("code", code);  
  61.    // 图象生效     
  62.    g.dispose();     
  63.    // 输出图象到页面     
  64.    ImageIO.write(image, "JPEG", response.getOutputStream());  
  65.    //加上下面代码,运行时才不会出现java.lang.IllegalStateException: getOutputStream() has already been called ..........等异常  
  66.    response.getOutputStream().flush();    
  67.    response.getOutputStream().close();    
  68.    response.flushBuffer();    
  69.    out.clear();    
  70.    out = pageContext.pushBody();   
  71. %>  

     3. 验证码检查页面(check.jsp:检查验证码是否输入正确)

  72. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  73.     pageEncoding="UTF-8"%>  
  74. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  75. <html>  
  76. <head>  
  77. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  78. <title>验证</title>  
  79. <script type="text/javascript">  
  80.     function same(){  
  81.         loginForm.action = "login/login_login";  
  82.         loginForm.submit();  
  83.     }  
  84.     function different(){  
  85.         alert("请输入正确的验证码");  
  86.         location.href = "login.jsp";  
  87.     }  
  88. </script>  
  89. </head>  
  90. <body>  
  91.     <%  
  92.     String userName = request.getParameter("userName");  
  93.     String password = request.getParameter("userPwd");  
  94.     %>  
  95.     <form id="loginForm" name="loginForm" method="post">  
  96.         <input type="hidden" id="userName" value=<%=userName %>  name="user.userName">  
  97.         <input type="hidden" id="userPwd" value=<%=password %> name="user.userPwd">  
  98.     </form>  
  99.     <%  
  100.         String rand = session.getAttribute("code").toString();  
  101.         String input = request.getParameter("code");  
  102.         if(rand.equals(input)){  
  103.             out.println("<script language='javascript'>same();</script>");  
  104.         }else{  
  105.             out.print("<script language='javascript'>different();</script>");  
  106.         }  
  107.     %>  
  108.         
  109. </body>  
  110. </html>  

     

猜你喜欢

转载自www.cnblogs.com/zqoceean/p/9461490.html
今日推荐