一个登陆模块。想输入验证码进行验证!一直难道我,请教高手指点

解决方案 »

  1.   

    http://www.cnblogs.com/jqyp/archive/2010/09/01/1815006.html
      

  2.   

    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    int height = 20;
    int width = 60;

    //创造一个图片缓冲流,包含所有的图片信息,相当于画布
    BufferedImage buimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buimg.createGraphics();//创建一个绘图工具

    g.setColor(Color.WHITE);//设置底色
    g.fillRect(0, 0, width, height);//开始填充

    g.setColor(Color.RED);//设置边框颜色
    g.drawRect(0, 0, width-1, height-1);//绘制边框

    Random rand = new Random();
    g.setColor(Color.GRAY);//设置干扰线的颜色
    for (int i = 1; i <= 20; i++) {
    int x1 = rand.nextInt(width);
    int y1 = rand.nextInt(height);
    int x2 = rand.nextInt(width);
    int y2 = rand.nextInt(height);
    g.drawLine(x1, y1, x2, y2);//绘制干扰线
    }

    String code = "";//用来记录生成的验证码
    int red=0,green=0,blue=0;//记录三原色的值,用来变换验证码的颜色
    Font f = new Font("Times New Roman",Font.BOLD,18);//创建一个字体对象
    g.setFont(f);//设置验证码的字体
    for (int i = 1; i <= 4; i++) {
    String num = rand.nextInt(10)+"";//随机生成验证码中的一个数字
    // System.out.println(num);
    //随机生成三原色
    red = rand.nextInt(100);
    green = rand.nextInt(100);
    blue = rand.nextInt(100);
    g.setColor(new Color(red,green,blue));//设置验证码的颜色
    g.drawString(num,11*i,16);//绘制验证码
    code+=num;//拼接四个验证码到一起
    }
    // System.out.println(code);
    HttpSession session = request.getSession();
    session.setAttribute("checkcode", code);//将验证码放入到session中,以便于登录检查使用

    //获得输出流,把图片发送给IE
    ServletOutputStream sos = response.getOutputStream();
    ImageIO.write(buimg,"jpeg", sos);//开始发送

    sos.flush();
    sos.close();//关闭流


    }
    这个是在Servlet里面写的都有注释,看不看得明白?
      

  3.   

    验证码:<input type="text" name="code" /><img style="cursor:hand;" title="看不清,换一张" onclick="changImg(this)" src="checkcode" /><br>
    <script type="text/javascript">
       function changImg(img)
       {
        img.src = "checkcode?"+new Date().getTime();
       }
       </script>这个是JSP页面的,看到那个IMG里面的src没有?路径就是servlet的名字。。
      

  4.   

    3楼代码的思路是对的,就是用image类,如果考虑需要旋转,可以使用graphics2D这个类,他里面有旋转的方法rotate(),还有如果需要产生随机字母,也可以用random.nextInt(26)+'a'来得到
      

  5.   

    这是用Jsp页面做的<%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜色
      Random random = new Random();
      if (fc > 255)
       fc = 255;
      if (bc > 255)
       bc = 255;
      int r = fc + random.nextInt(bc - fc);
      int g = fc + random.nextInt(bc - fc);
      int b = fc + random.nextInt(bc - fc);
      return new Color(r, g, b);
     }%>
    <%      
     //设置页面不缓存
     response.setHeader("Pragma", "No-cache");
     response.setHeader("Cache-Control", "no-cache");
     response.setDateHeader("Expires", 0); // 在内存中创建图象
     int width = 60, height = 20;
     BufferedImage image = new BufferedImage(width, height,
       BufferedImage.TYPE_INT_RGB); // 获取图形上下文
     Graphics g = image.getGraphics(); //生成随机类
     Random random = new Random(); // 设定背景色
     g.setColor(getRandColor(200, 250));
     g.fillRect(0, 0, width, height); //设定字体
     g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); //画边框
     //g.setColor(new Color());
     //g.drawRect(0,0,width-1,height-1); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
     g.setColor(getRandColor(160, 200));
     for (int i = 0; i < 155; i++) {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      g.drawLine(x, y, x + xl, y + yl);
     } // 取随机产生的认证码(4位数字)
     String sRand = "";
     for (int i = 0; i < 4; i++) {
      String rand = String.valueOf(random.nextInt(10));
      sRand += rand;
      // 将认证码显示到图象中
      g.setColor(new Color(20 + random.nextInt(110), 20 + random
        .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
      g.drawString(rand, 13 * i + 6, 16);
     } // 将认证码存入SESSION
     session.setAttribute("rand", sRand); // 图象生效
     g.dispose();
     OutputStream output = response.getOutputStream(); // 输出图象到页面
     ImageIO.write(image, "JPEG", response.getOutputStream());
     output.flush();
     out.clear();
     out = pageContext.pushBody();
    %>