验证码的代码如何写

解决方案 »

  1.   

    你要用什么写?js和java一些都可以写的
      

  2.   

    公司一般使用java 还是js呀
      

  3.   

    用开源工具吧 jCaptcha, patcha 都是非常优秀的!
      

  4.   

    最简单的就是 设置<td><%=code%></td>这个td的背景颜色为一个模糊的验证码图片 数字不要 数字可以直接在前台通过随机数方法生成 验证过程可以放在前台后台都可以
      

  5.   

    最简单的就是 设置<td><%=code%></td>这个td的背景颜色为一个模糊的验证码图片 数字不要 数字可以直接在前台通过随机数方法生成 验证过程可以放在前台后台都可以
      

  6.   

    最简单的就是 设置<td><%=code%></td>这个td的背景颜色为一个模糊的验证码图片 数字不要 数字可以直接在前台通过随机数方法生成 验证过程可以放在前台后台都可以
      

  7.   

    http://blog.csdn.net/zxingchao2009/archive/2010/08/17/5819411.aspx验证码可以用servlet来生成,详细请参考上面的
      

  8.   


    我用过java的,这个关注一下。
      

  9.   


    jCaptcha 和 patcha 都是 Java 的啊jCaptcha: http://jcaptcha.sourceforge.net/
    jCaptcha 中 Gmail 式的验证码:
    patcha: http://code.google.com/p/patchca/
    我比较喜欢这个,比 jCaptcha 小巧,也没那么花哨,这是示例:
      

  10.   

    纠正一下是 patchca,不是 patcha,呵呵
      

  11.   

    //验证码package com.user;import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.awt.*;
    import java.awt.image.*;
    import java.util.*;
    import javax.imageio.*;
    public class Image extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse res ) throws ServletException, IOException {
    //res.setContentType("text/html");
    //res.getOutputStream().print("hello");




    //设置页面不缓存
    res.setHeader("Pragma","No-cache");
    res.setHeader("Cache-Control","no-cache");
    res.setDateHeader("Expires", 0);
    //定义图像宽度和高度
    int width = 85;
    int height = 20;
    //创建图片
    BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics();


    Random random = new Random(); //底色
    g.setColor(getRandColor(200,250));
    g.fillRect(0,0,width,height);
    //字体
    g.setFont(new Font("华文彩云",Font.PLAIN,18));

    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);
    }
    String sRand="";
    for (int i=0;i<6;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);
    }
    HttpSession session = request.getSession(true); 
    session.setAttribute("rand",sRand);
    session.setMaxInactiveInterval(900);
    g.dispose();
    ImageIO.write(img, "JPEG", res.getOutputStream()); }
    public void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
    doGet(request,response );
    }
    //取得随机颜色
    public 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);
    }
    }
      

  12.   


    Java code
    package com.city.sxzlc.servlet;import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.util.Random;import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class AuthenticationCode extends HttpServlet {
        public AuthenticationCode() {
            super();
        }
        public void destroy() {
            super.destroy(); // Just puts "destroy" string in log
            // Put your code here
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {         //验证码
            int width=70;
            int height=20;
            Integer[] a = new Integer[4];
            String randStr="";
            Random random = new Random();
            Color c = new Color(34,56,78);
            for(int i=0;i<4;i++)
                a[i] = random.nextInt(10);
            for(Integer ele:a)
                randStr+=ele;
            System.out.println(randStr);
            
            
            try{ 
            response.setContentType("image/jpeg");
            OutputStream out = response.getOutputStream();
            BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); //feredImage(int width, int height, int imageType) 
            Graphics g = image.getGraphics();
            g.fillRect(1, 1, width, height);
            g.setColor(c);
            
            int i=5;
            i=5+random.nextInt(20);
            for(Integer ele:a) {//一个一个画
                g.drawString(ele.toString(), i+random.nextInt(5), 10+random.nextInt(10));
                i=i+8;
            }
            
            ImageIO.write(image, "jpeg", out);
            }catch(Exception e){e.printStackTrace();}
            request.getSession().setAttribute("authCode", randStr);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {        this.doGet(request, response);
        }
        public void init() throws ServletException {
        
        }}
      

  13.   

             <!-- 验证码  --> 
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>AuthenticationCode</servlet-name>
        <servlet-class>com.city.sxzlc.servlet.AuthenticationCode</servlet-class>
      </servlet>          <!-- 验证码     mapping  --> 
      <servlet-mapping>
        <servlet-name>AuthenticationCode</servlet-name>
        <url-pattern>/login/AuthenticationCode</url-pattern>
      </servlet-mapping>
      

  14.   

    <tr><td>验证码</td><td><input name="authCode" size=5><img src="../login/AuthenticationCode" alt=验证码 id=authCodeID></td><td></td></tr>