JavaScript 和 JSP
当我尝试在 javascript 中访问 jsp 变量时,我总是将其设置为 null。为什么会这样呢?我如何在javascript中获取实际的jsp变量值。这是
<%! 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>
弹出警报框时我的代码..它为 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 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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为在提交表单之前,JSP 变量已被打印为 JavaScript 变量。
简短说明:JSP在Web服务器上运行,生成HTML/CSS/JS,Web服务器将其发送到Web浏览器,HTML/CSS/JS在Web浏览器上运行。详细解释:Java/JSP 和 JavaScript 之间的通信。
如何解决这个问题:用 Java Servlet 替换 JavaScript。 JavaScript 不是处理请求的合适工具。
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.
您需要移动:
到代码顶部(或该行上方的某个位置: var oldpassword_actual="<%= oldpassword %>";)
You need to move:
To the top of your code (or somewhere above the line: var oldpassword_actual="<%= oldpassword %>";)