JSP登录页面包括验证码的验证

login.jsp,代码如下:

[plain]  view plain  copy
 print ?
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'login.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.   <%  
  25.     String incode = (String)request.getParameter("code");   
  26.     String rightcode = (String)session.getAttribute("rCode");   
  27.       
  28.     if(incode != null && rightcode != null){  
  29.         if(incode.equals(rightcode)){  
  30.             out.println("验证码输入正确!");  
  31.         }else{  
  32.             out.println("验证码输入不正确,请重新输入!");  
  33.         }  
  34.     }  
  35.   %>  
  36.     
  37.   <body>  
  38.     <form action="login.jsp" method="post">  
  39.     用户名:  
  40.     <input type="text" name="username"/><br/>  
  41.     密码:  
  42.     <input type="password" name="password"/><br/>  
  43.     验证码:  
  44.     <img src="number.jsp"/>  
  45.     <input type="text" name="code"/>  
  46.     <input type="submit" value="登录"/>  
  47.     </form>  
  48.   </body>  
  49. </html>  

number.jsp提供了验证码的提取功能,如下:
[plain]  view plain  copy
 print ?
  1. <%@ page contentType="image/jpeg" language="java" import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" pageEncoding="utf-8"%>  
  2.   
  3. <%!  
  4.     Color getRandColor(int fc,int bc){  
  5.         Random random = new Random();  
  6.         if(fc > 255){  
  7.             fc = 255;  
  8.         }  
  9.         if(bc < 255){  
  10.             bc = 255;  
  11.         }  
  12.         int r = fc +random.nextInt(bc-fc);  
  13.         int g = fc +random.nextInt(bc-fc);  
  14.         int b = fc +random.nextInt(bc-fc);  
  15.           
  16.           
  17.         return new Color(r,g,b);  
  18.     }  
  19. %>  
  20.   
  21. <%   
  22.     //设置页面不缓存  
  23.     response.setHeader("Pragma","no-cache");  
  24.     response.setHeader("Cache-Control","no-catch");  
  25.     response.setDateHeader("Expires",0);  
  26.       
  27.     //在内存中创建图象  
  28.     int width = 60;  
  29.     int height = 20;  
  30.     BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
  31.       
  32.     //创建图象  
  33.     Graphics g = image.getGraphics();  
  34.     //生成随机对象  
  35.     Random random = new Random();  
  36.     //设置背景色  
  37.     g.setColor(getRandColor(200,250));  
  38.     g.fillRect(0,0,width,height);  
  39.     //设置字体  
  40.     g.setFont(new Font("Tines Nev Roman",Font.PLAIN,18));  
  41.     //随机产生干扰线  
  42.     g.setColor(getRandColor(160,200));  
  43.     for(int i = 0; i < 255; i++){  
  44.         int x = random.nextInt(width);  
  45.         int y = random.nextInt(height);  
  46.         int xl = random.nextInt(12);  
  47.         int yl = random.nextInt(12);  
  48.     }  
  49.     //随机产生认证码,4位数字  
  50.     String sRand = "";  
  51.     for(int i = 0; i < 4; i++){  
  52.         String rand = String.valueOf(random.nextInt(10));  
  53.         sRand  += rand;  
  54.         //将认证码显示到图象中  
  55.         g.setColor(new Color(20 + random.nextInt(110),20 + random.nextInt(110),20 + random.nextInt(110)));  
  56.         g.drawString(rand,13*i+6,16);  
  57.     }  
  58.     session.setAttribute("rCode",sRand);  
  59.     //图像生效  
  60.     g.dispose();  
  61.     //输出图像到页面  
  62.     ImageIO.write(image,"JPEG",response.getOutputStream());  
  63.     out.clear();  
  64.     out = pageContext.pushBody();  
  65. %>  


  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值