admin管理员组文章数量:1336660
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 02 Answers
Reset to default 2It'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 %>";)
本文标签: Javascript and JSPStack Overflow
版权声明:本文标题:javascript and jsp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742418023a2471096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论