登陆页面当中调用session取值: <%
String ccode=(String)request.getSession().getAttribute("ccode");
System.out.println("Jsp-->"+ccode);
 %>form表单:
<html:form action="jsp/user.do" method="post">
用户ID:<html:text property="userid" onblur="checkUserName();"></html:text>
<span id="userIdMessage" style="width:200px ; height:10px;"></span>
<br>
用户密码:<html:password property="userpwd" onblur="checkPassWord();"></html:password>
<span id="pwdMessage" style="width:200px ; height:10px;"></span>
<br>
确认密码:<html:password property="confirmpwd" onblur="checkConfpwd();"></html:password>
<span id="confpwdMessage" style="width:200px ; height:10px;"></span>
<br>
丢失密码提示问题:<html:text property="userques"></html:text>
<br>
丢失密码问题答案:<html:text property="userans"></html:text>
<br>
验证码:<html:text property="checkcode" onblur="checkCode();"></html:text>
<img src="image.jsp">
<input type="hidden" name="ccode" value="<%=ccode%>">
<span id="checkcodeMessage" style="width:200px ; height:10px;"></span>
<br>
<input type="hidden" name="status" value="register">
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
验证码生成代码主要就是:// 将认证码存入SESSION
session.setAttribute("ccode",sRand);问题:第一次到达页面,从session里面没有取到验证码值,以后刷新都是取到上次显示的值。
(注:我这个是struts+dwr做的,望高人指点迷津)

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【yudylaw】截止到2008-06-30 10:33:30的历史汇总数据(不包括此帖):
    发帖数:3                  发帖分:200                
    结贴数:2                  结贴分:160                
    未结数:1                  未结分:40                 
    结贴率:66.67 %            结分率:80.00 %            
    楼主加油
      

  2.   

    都是这样的,
    你把两个页面分开用就可以了,就是把提交页和接收页面分开,
    如果用一个页面的话,就在页面里判断一下是否是提交状态!
    if( request.getMethod().equalsIgnoreCase("POST") == true )
      

  3.   

    应该是浏览器缓存的事儿,试试改成
    <img src="image.jsp?<%=Long.toString(System.currentTimeMillis(), 36)%>">
    另外有了这句
    <input type="hidden" name="ccode" value="<%=ccode%>">
    你的验证码就跟没有一样,起不到作用。
      

  4.   

    页面代码:<script type='text/javascript'  src='<%=request.getContextPath()%>/dwr/engine.js'> </script>
    <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'> </script>
    <script type='text/javascript'  src='<%=request.getContextPath()%>/dwr/interface/ajaxValidateDAO.js'> </script>
    <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/user.js'> </script>
    <%
    String ccode=(String)request.getSession().getAttribute("ccode");
    System.out.println("Jsp-->"+ccode);
     %>
    <script type="text/javascript">
    function checkUserName()
    {
    var userName=dwr.util.getValue("userid");
    //var passWord = dwr.util.getValue("userpwd");
    //User ={userid:userid,userpwd:userpwd};
    ajaxValidateDAO.checkUserName(userName,
    function(data)
    {
    if(data!="用户名可用")
    {
    document.userForm.userid.value="";
    document.userForm.userid.focus();
    }
    dwr.util.setValue("userIdMessage",data);
    }
    );
    }

    function checkPassWord()
    {
    var passWord = dwr.util.getValue("userpwd");
    //var userName=dwr.util.getValue("userid");
    ajaxValidateDAO.checkPassword(passWord,
    function(data)
    {
    if(data!="密码输入正确")
    {
    document.userForm.userpwd.value="";
    document.userForm.userpwd.focus();
    }
    dwr.util.setValue("pwdMessage",data);
    }
    );
    }

    function checkConfpwd()
    {
    var confpwd = dwr.util.getValue("confirmpwd");
    var passWord = dwr.util.getValue("userpwd");
    ajaxValidateDAO.confPassword(passWord,confpwd,
    function(data)
    {
    if(data!="密码输入正确")
    {
    document.userForm.userpwd.value="";
    document.userForm.confirmpwd.value="";
    document.userForm.userpwd.focus();
    }
    dwr.util.setValue("confpwdMessage",data);
    }
    );
    }

    function checkCode()
    {
    var ccode =dwr.util.getValue("ccode");
    // alert(ccode);
    var checkcode=dwr.util.getValue("checkcode");
    ajaxValidateDAO.checkCode(checkcode,ccode,
    function(data)
    {
    if(data=!"验证码输入正确")
    {
    document.userForm.checkcode.value="";
    document.userFrom.checkcode.focus();
    }
    }
    dwr.util.setValue("checkcodeMessage",data);
    );
    }
    </script>
    </head>
    <body>
    <center>
    <jsp:include flush="true" page="../inc/template.jsp">
    <jsp:param name="url" value="../"/>
    </jsp:include> 
    <h1>用户注册</h1>
    <html:form action="jsp/user.do" method="post">
    用户ID:<html:text property="userid" onblur="checkUserName();"></html:text>
    <span id="userIdMessage" style="width:200px ; height:10px;"></span>
    <br>
    用户密码:<html:password property="userpwd" onblur="checkPassWord();"></html:password>
    <span id="pwdMessage" style="width:200px ; height:10px;"></span>
    <br>
    确认密码:<html:password property="confirmpwd" onblur="checkConfpwd();"></html:password>
    <span id="confpwdMessage" style="width:200px ; height:10px;"></span>
    <br>
    丢失密码提示问题:<html:text property="userques"></html:text>
    <br>
    丢失密码问题答案:<html:text property="userans"></html:text>
    <br>
    验证码:<html:text property="checkcode" onblur="checkCode();"></html:text>
    <img src="image.jsp?<%=Long.toString(System.currentTimeMillis(), 36)%>">
    <input type="hidden" name="ccode" value="<%=ccode%>">
    <span id="checkcodeMessage" style="width:200px ; height:10px;"></span>
    <br>
    <input type="hidden" name="status" value="register">
    <input type="hidden" name="type" value="1">
    <html:submit value="注册"></html:submit>
    <html:reset value="重置"></html:reset>
    </html:form>
      

  5.   

    session.setAttribute("ccode",sRand); 这句写哪儿了?
      

  6.   

    img.jsp当中,这个是生成验证码的页面。单独的。
      

  7.   

    原因可能是验证码的还有没生成,你就在session当中拿了值,所以第一次就没有拿到。第二次~~~~~之后拿的就是上一次的值。。
       你可另建一个servlet去session当中拿值试试。          
      

  8.   

    ---问题我换了一个方式解决了,验证码是在提交的时候才给servlet,我就到servlet中验证验证码了,没有在页面中做。然后把servlet的处理结果返回给前台页面,做相应处理。
      

  9.   

    这个问题:就是一个时间差的问题。。
    首先,页面上的session会先于图片加载过来。所以在随机数还没生成的时候,session就被读到jsp上了.这就解释了为什么第一次session里null,而后面每次都是上一次的值。上面的其实都是屁话;真正解决问题的方法是:
    比如:你的验证码是在A.jsp上的文本框里输入的,触发一个onblur事件。这个事件对应一个 ajax 动态的在servlet里验证,session里的值和你输入值是否向相等。在用PrintWriter写会页面。会ajax的这点都能做到。因为,其实当图片加载过来时,session里的值已经被更新,只是页面上无法及时的更新罢了。这时ajax的异步刷新就发挥作用了。下面贴源代码,希望对楼主有所帮助:
     
    页面代码jsp:
     
        <font size="2">验证码 :</font>   
            <img id="checkCodeImage" src="servlet/ValidateCodeServllet" style="cursor:pointer" onclick="javascript:ajaxMethod('checkCodeImage');"/>  
            <a style="cursor:pointer" onclick="javascript:ajaxMethod('checkCodeImage');">
            <font id="fontId" size="2">
            <u onmouseover="colorblue('fontId');" onmouseout="colorblack('fontId');">看不清换一个</u></font>
            </a> 
            <BR>
            
            <input type="text" size=30 onchange="validate(this.value)"> <span id="va"></span>脚本代码:
    <script type="text/javascript">  
            function ajaxMethod(cid){  
                document.getElementById(cid).setAttribute("src",null);  
                //加上随机数的目的是为了每次都改变url 让浏览器不要加载缓存  
                var svalue = "servlet/ValidateCodeServllet?id=" + Math.random();  
                document.getElementById(cid).setAttribute("src",svalue);                  
            }  
            function colorblue(id){  
                document.getElementById(id).color="blue";  
            }  
            function colorblack(id){  
                document.getElementById(id).color="black";  
            }  
         function validate(e){
        
          if(window.ActiveXObject){
          
          xmlhttp = new ActiveXObject("Microsoft.XMLHttp")
          }else{
          xmlhttp = new XMLHttpRequest();
        
          
          }
        
          xmlhttp.open("get","servlet/CheckVa?str="+e);
          xmlhttp.onreadystatechange = handlerequest;
           xmlhttp.send(null);
        
        
         }
         function handlerequest(){
     
      if(xmlhttp.readyState==4){
    var sp = document.getElementById("va");
    var rr = xmlhttp.responseText;

    sp.innerHTML =rr;
      
      }


    }
        </script>  servlet代码:
    public class CheckVa extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String string = request.getParameter("str");
    System.out.println(string+"!@#$%^&*");
    HttpSession session = request.getSession();
    System.out.println(session.getAttribute("check_code")+"这是验证框架里的session");
    PrintWriter pw = response.getWriter();
    if(string .equals(session.getAttribute("check_code"))){
    pw.print("对@@@@@@@@@@@@@@");

    }else{

    pw.print("错@@@@@@@@@@@@@@");
    }
    pw.flush();
    pw.close();
    }}