各位哥哥姐姐们,我想问个关于验证码的问题,我在登录页面输入的验证码,怎么才能验证输入的验证码与显示的验证码是一直的呢?谢谢

解决方案 »

  1.   

    把后台生成的验证码放入session,每次登陆的时候去session里面取出来对比一下
      

  2.   

    生成验证码的页面,在session里保存验证码
    session.setAttribute("code", [验证码对象]);
    验证页面再从session里取出来
    session.getAtrribute("code");
    然后再用它来和从上个页面填写提交过来的比较即可.
      

  3.   

    HttpSession session=request.getSession();
    String sRand =(String)session.getAttribute("rand");
    String certCode=NewUserForm.getCertCode();
    if(!sRand.equals(certCode)){
    result="验证码错误";
    request.setAttribute("result", result);
    return new ActionForward("/newUser.jsp");
    }
      

  4.   

    把生成的验证码不光要显示在页面上,还要把其保存下来,最方便的就是放入SESSION中.
    在ACTION或是SERVLET中用时就可以直接取出了.
      

  5.   

    没明白啊,咋往session里存啊?session我就不怎么明白怎么回事啊
      

  6.   

    3楼已经写得很明白了!生成验证码的页面,在session里保存验证码:
    session.setAttribute("code", [验证码对象]);后台或验证页面再从session里取出来:
    session.getAtrribute("code"); 
      

  7.   


    生成验证码的页面,在session里保存验证码 
    session.setAttribute("code", [验证码对象]); 
    验证页面再从session里取出来 
    session.getAtrribute("code"); 
    然后再用它来和从上个页面填写提交过来的比较即可.
     
    就是这么个原理 你试试啊
      

  8.   

    还是不会写啊,我写完不管输入啥都说我输入错误 :(
    这是登录页面的代码
    <script language="JavaScript" type="text/JavaScript">
           
                  function changeCode(url){
                    var cc = document.getElementById("pic");
                     cc.src=url+"?"+Math.random();             
                  }
                  
        </script>
    <script>
    </script>
    <body>   
    <form action="login" method="post">
      <table id="logo" align="center">
        <tr>
      <td>
        <font color="#000033" size="+4" face="华文彩云">安全管理系统</font>
      </td>
    </tr>
      </table>
      <table width="60%" height="98%" align="center" background="./photo/bg_4.gif">
        <tr>
      <td>
        <table align="center">
      <tr>
         <td>用户名</td>
     <td><input type="text" name="operatorName"/></td>
      </tr>
      <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
      </tr>
      <tr>
        <td>密&nbsp;&nbsp;&nbsp;&nbsp;码</td>
    <td><input type="password" name="password"/></td>
      </tr>
      <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
      </tr>
      <tr>
        <td align="right" valign="top">验证码</td>
    <td valign="top">
        <input type="text" name="c_code"> <br>
    <img id="pic" border=1 src="yzm.jsp" >
    <a href="javaScript:changeCode('yzm.jsp');">看不清,换一张?</a>
    </td>
      </tr>
      <tr>
        <td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" name="submit" value="登录">
        <input type="button" name="button" value="取消" onClick="window.close()">
        <a href="<s:url action="/WEB-INF/jsp/forget_pass.jsp"/>"><input type="button" name="button1" value="忘记密码"></a></td>
      </tr>
    </table>
      </td>
    </tr>
      </table>
    </form>
       </body>
    这是登录以后跳转到的页面代码
    <head>  
        <title>安全管理</title>
        <meta  http-equiv="Content-Type" content="text/html; charset=utf-8">
      </head>
      <frameset rows="77,*" framespacing="0" frameborder="no" border="0" id="s">
       <frame src="top.jsp" name="top" scrolling="no" noresize marginwidth="0" marginheight="0" id="top">
       <frameset cols="200,*" frameborder="no" id="l">
        <frame src="onLeft.jsp" name="leftFrame" scrolling="no" noresize marginwidth="0" marginheight="0">
        <frame src="main.jsp" name="mainFrame"  noresize marginwidth="0" marginheight="0">
        <frame src="/UntitledFrame-1.html">
       </frameset>
      </frameset>
      <noframes><body>
      </body></noframes>
    </html>
    麻烦帮我看看呗?谢谢了
      

  9.   

    <%@ page language="java" import="java.util.*" 
     import="www.eternal.com.*"
    pageEncoding="utf-8" contentType="image/jpeg" import="java.awt.*,
    java.awt.image.*,java.util.*,javax.imageio.*" %>
    <%
    //设置页面不缓存
    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(ImageTools.getRandColor(200,250));
    g.fillRect(0,0,width,height);

    //设定字体
    g.setFont(new Font("Times New Roman",Font.PLAIN,18));

    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
    g.setColor(ImageTools.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);  ------------------------------------------------------- // 这里存入session

    // 图象生效
    g.dispose();

    // 输出图象到页面
    ImageIO.write(image,"JPEG",response.getOutputStream());
    out.clear();
    out = pageContext.pushBody();
    %>
      

  10.   

    验证码: <input type="text" name="rand"><br>
    <img src="images.jsp" title="验证码,看不清楚?请点击刷新验证码" style="cursor:pointer" onClick="this.src='images.jsp?'+Math.random()" />请点图片刷新验证码<br>
      

  11.   


    public class ImageTools 
    {
        public static 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);
        }
    }