java jsp实现登录验证码

工作中使用的java jsp实现的登录验证码。代码记录如下:

1.登录主页面(login.jsp)

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

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

Java代码   收藏代码
  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:检查验证码是否输入正确)

Java代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>验证</title>  
  8. <script type="text/javascript">  
  9.     function same(){  
  10.         loginForm.action = "login/login_login";  
  11.         loginForm.submit();  
  12.     }  
  13.     function different(){  
  14.         alert("请输入正确的验证码");  
  15.         location.href = "login.jsp";  
  16.     }  
  17. </script>  
  18. </head>  
  19. <body>  
  20.     <%  
  21.     String userName = request.getParameter("userName");  
  22.     String password = request.getParameter("userPwd");  
  23.     %>  
  24.     <form id="loginForm" name="loginForm" method="post">  
  25.         <input type="hidden" id="userName" value=<%=userName %>  name="user.userName">  
  26.         <input type="hidden" id="userPwd" value=<%=password %> name="user.userPwd">  
  27.     </form>  
  28.     <%  
  29.         String rand = session.getAttribute("code").toString();  
  30.         String input = request.getParameter("code");  
  31.         if(rand.equals(input)){  
  32.             out.println("<script language='javascript'>same();</script>");  
  33.         }else{  
  34.             out.print("<script language='javascript'>different();</script>");  
  35.         }  
  36.     %>  
  37.       
  38. </body>  
  39. </html>  

转自:http://a455360448201209214217.iteye.com/blog/1953785
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值