【JSP】JSP彩色验证码的实现

image.jsp----------------------------------------------------------------------

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
  7.     <%!
  8.     Color getRandColor(int fc,int bc){//给定范围获得随机颜色
  9.             Random random = new Random();
  10.             if(fc>255) fc=255;
  11.             if(bc>255) bc=255;
  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.             return new Color(r,g,b);
  16.             }
  17.     %>
  18.     <%
  19.     //设置页面不缓存
  20.     response.setHeader("Pragma","No-cache");
  21.     response.setHeader("Cache-Control","no-cache");
  22.     response.setDateHeader("Expires", 0);
  23.     // 在内存中创建图象
  24.     int width=60, height=20;
  25.     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  26.     // 获取图形上下文
  27.     Graphics g = image.getGraphics();
  28.     //生成随机类
  29.     Random random = new Random();
  30.     // 设定背景色
  31.     g.setColor(getRandColor(200,250));
  32.     g.fillRect(0, 0, width, height);
  33.     //设定字体
  34.     g.setFont(new Font("Times New Roman",Font.PLAIN,18));
  35.     //画边框
  36.     //g.setColor(new Color());
  37.     //g.drawRect(0,0,width-1,height-1);
  38.     // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
  39.     g.setColor(getRandColor(160,200));
  40.     for (int i=0;i<155;i++)
  41.     {
  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 sRand="";
  50.     for (int i=0;i<4;i++){
  51.         String rand=String.valueOf(random.nextInt(10));
  52.         sRand+=rand;
  53.         // 将认证码显示到图象中
  54.         g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  55.         g.drawString(rand,13*i+6,16);
  56.     }
  57.     // 将认证码存入SESSION
  58.     session.setAttribute("rand",sRand);
  59.     // 图象生效
  60.     g.dispose();
  61.     // 输出图象到页面
  62.     ImageIO.write(image, "JPEG", response.getOutputStream());
  63.     %>

a.jsp------------------------------------------------------------------------------------------------------

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  2. <%
  3.  String path = request.getContextPath();
  4.  String basePath = request.getScheme() + "://"
  5.    + request.getServerName() + ":" + request.getServerPort()
  6.    + path + "/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10.  <head>
  11.   <title>认证码输入页面</title>
  12.   <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  13.   <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  14.   <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  15.   <META HTTP-EQUIV="Expires" CONTENT="0">
  16.  </head>
  17.  <body>
  18.   <form method=post action="check.jsp">
  19.    <table>
  20.     <tr>
  21.      <td align=left>
  22.       系统产生的认证码:
  23.      </td>
  24.      <td>
  25.       <img border=0 src="image.jsp">
  26.      </td>
  27.     </tr>
  28.     <tr>
  29.      <td align=left>
  30.       输入上面的认证码:
  31.      </td>
  32.      <td>
  33.       <input type=text name=rand maxlength=4 value="">
  34.      </td>
  35.     </tr>
  36.     <tr>
  37.      <td colspan=2 align=center>
  38.       <input type=submit value="提交检测">
  39.      </td>
  40.     </tr>
  41.     </table>
  42.     </form>
  43.  </body>
  44. </html>

check.jsp----------------------------------------------------------------------------------------------------------------------

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <%@ page contentType="text/html; charset=gb2312" language="java"
  8.  import="java.sql.*" errorPage=""%>
  9. <html>
  10.  <head>
  11.   <title>认证码验证页面</title>
  12.   <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  13.   <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  14.   <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  15.   <META HTTP-EQUIV="Expires" CONTENT="0">
  16.  </head>
  17.  <body>
  18.   <%
  19.    String rand = (String) session.getAttribute("rand");
  20.    String input = request.getParameter("rand");
  21.   %>
  22.   系统产生的认证码为:
  23.   <%=rand%>
  24.   <br>
  25.   您输入的认证码为:
  26.   <%=input%>
  27.   <br>
  28.   <br>
  29.   <%
  30.   if (rand.equals(input)) {
  31.   %>
  32.   <font color=green>输入相同,认证成功!</font>
  33.   <%
  34.   } else {
  35.   %>
  36.   <font color=red>输入不同,认证失败!</font>
  37.   <%
  38.   }
  39.   %>
  40.  </body>
  41. </html>
  42. QQ:343269876
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值