怎么在表单前验证用户输入的验证码是否输入正确?不提交到后台验证
验证码生成后,放在session对象 中,在线等 ,附代码最好

解决方案 »

  1.   

    要在JS中验证的话,后台生成验证码后,必须要放到前台页面(作为隐藏域、或者JS变量)
      

  2.   

    <img id="img" src="code.jsp" >我是在前台用图片的SRC链接验证码的JSP但在前台用SESSION取验证码的时候 ,取到的验证码老是前一张,不能同步
      

  3.   

    取到老一张的图片,是因为<img id="img" src="code.jsp" >要在加载图片的时候才会去调用code.jsp
    而这个时候首页的jsp在服务器端已经执行,并生成页面到客户端
    所以取的是之前一张的图片
    LZ可以用ajax再调用一个JSP,在那个JSP里取SESSION
      

  4.   


    没其它办法吗?我们还没学到ajax= =! 
      

  5.   

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
                ServletException, IOException {
            response.setContentType(CONTENT_TYPE);
            Random ran=new Random();
            ServletOutputStream out=response.getOutputStream();
            BufferedImage img=new BufferedImage(60,30,BufferedImage.TYPE_INT_RGB);
            Graphics g=img.getGraphics();
            //g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
            g.fillRect(0,0,60,30);
            g.setFont(new Font("黑体",Font.PLAIN,25));
            g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
            StringBuffer str=new StringBuffer();
            for (int i = 0; i < 4 ; i++) {
                str.append((char)(ran.nextInt(26)+65));
            }
            g.drawString(str.toString(),5,25);
            for (int i = 0; i <= 10 ; i++) {
                //g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
                g.drawLine(ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()),ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()));
            }
            JPEGImageEncoder jpeg=JPEGCodec.createJPEGEncoder(out);
            jpeg.encode(img);
            out.close();
            request.getSession().setAttribute("code",str.toString());
        }
    上面是出验证码,是用servlet写的,,一样的
    <%@ page contentType="text/html; charset=GBK" %>
    <html>
    <head>
    <title>
    jsp1
    </title>
    <script type="">
     function change()
     {
       document.getElementById("imga").src="checkservlet?"+Math.random();
     }
    </script>
    </head>
    <body bgcolor="#ffffff">
    <h1>
    验证码
    </h1>
    <form method="post" action="jsp2.jsp">
    <br>
    <img id="imga" alt="" src="checkservlet" />
    <a href="javascript:change();">看不清楚,换一张</a>
    <br>
    验证码:<input type="text" name="cd"/>
    <input type="Submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>
    上面的是调用
    <%@page contentType="text/html; charset=GBK"%>
    <%
      String code = request.getParameter("cd");
      String check=session.getAttribute("code").toString();
      if (!code.equals(check)) {
        out.println("<script>alert('验证码错误!');history.back();</script>");
      }
      else {
        out.println("<script>alert('验证码正确!');history.back();</script>");
      }
    %>
    这个是处理,,你可以看一下
      

  6.   

    我也碰到了这个问题,有大神建议先用随机数生成验证码,再将验证码写到图片中去,这样session中存的便是数字,就不会出现刷新后获得的是前一个验证码了!