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