日期:2014-05-18  浏览次数:20738 次

大家帮帮忙 session.getAttribute很奇怪的问题.
小弟做图片验证,session.getAttribute取值,第一次值为NULL,第二次有值.
源代码如下:  
login.jsp
<div   id= "login ">
<form   name= "login "   method= "post "   action= "loginin.jsp "   onsubmit= "return   check(this); ">
<div   id= "user "> 用户   <input   type= "text "   name= "userid "   /> </div>
<div   id= "pass "> 密码   <input   type= "password "   name= "userpassword "   /> </div>
<div   id= "check "> 验证   <input   type= "text "   name= "checkcode "   maxlength= "4 "   /> <img   id= "vimg "   alt= "点击刷新 "   onclick= "reloadVimg() "   style= "cursor:hand "   src= "/vimage "/> </div>

loginin.jsp
if(session.getAttribute( "post_validate_code ")==null||!equest.getParameter( "checkcode ").equals(session.getAttribute( "post_validate_code ").toString()))
{ loginuser=null;
out.println( " <script   language= 'javascript '> alert( '验证码错误!! ');history.back(1); </script> ");
                        return;
                        }

vimage.java(这是一个servlet)
          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+2,15);
                                }
  session.setAttribute( "post_validate_code ",sRand);

------解决方案--------------------
第一次因还没有赋值当然是null了,第而次用session.setAttribute( "post_validate_code ",sRand);这条语句赋了值当然就有值了.
------解决方案--------------------
程序是从上至下运行的
------解决方案--------------------
<div id= "login ">
<form name= "login " method= "post " action= "loginin.jsp " onsubmit= "return check(this