javascript and jsp - Stack Overflow

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

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 password<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..

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 password<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..

Share Improve this question edited Jan 6, 2011 at 17:23 skaffman 404k96 gold badges824 silver badges775 bronze badges asked Jan 6, 2011 at 17:18 nikhilnikhil 9,37323 gold badges59 silver badges81 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

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: munication 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 %>";)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742418024a4440122.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信