开发者

javascript and jsp

when i try to access a jsp variable in javascript i always get it as null. why is this so? how do i get actual jsp variable value in javascript. here is my code

<%! String oldpassword; %>
<html>
    <head>
    <script type="text/javascript">
        function redirect()
        {
        var oldpassword_actual="<%= oldpassword %>";
        var oldpassword_entered=document.form.oldpassword.value;
        var newpassword=document.form.newpassword.value;
        var reenterpassword=document.form.confirmpassword.value;

        alert(oldpassword_actual);
        alert(oldpassword_entered);
        alert(newpassword);
        alert(reenterpassword);
        return false;
        }
    </script>
    </head>
    <body align="center">
    <form name="form" action="" method="post">
            Enter old p开发者_如何转开发assword<input type="password" name="oldpassword"></br></br>
            Enter new password<input type="password" name="newpassword"></br></br>
            Reenter new password<input type="password" name="confirmpassword"></br></br>
        <%
        oldpassword=(String)session.getAttribute("Password");
        %>
            <input type="submit" name="confirm" value="Confirm" onclick="return redirect()"> 
    </form>
    </body>
</html>

when the alert box pops up..it gives a null value for the jsp variable..


It's because the JSP variable is been printed as JavaScript variable before the form is submitted.

Short explanation: JSP runs at webserver, produces HTML/CSS/JS, webserver sends it to webbrowser, HTML/CSS/JS runs at webbrowser. Long explanation: communication between Java/JSP and JavaScript.

How to solve this: replace JavaScript by a Java Servlet. JavaScript isn't the right tool for the job of request processing.


You need to move:

    <%
    oldpassword=(String)session.getAttribute("Password");
    %>

To the top of your code (or somewhere above the line: var oldpassword_actual="<%= oldpassword %>";)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜