如何在 JSP 中访问 javascript 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3116058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i access javascript variables in JSP?
提问by Tushar Ahirrao
I want to access Javascript variables in JSP code. How can I do that?
我想在 JSP 代码中访问 Javascript 变量。我怎样才能做到这一点?
采纳答案by SageNS
JavaScript variable is on client side, JSP variables is on server side, so you can't access javascript variables in JSP. But you can store needed data in hidden fields, set its value in client and get it on server over GET or POST.
JavaScript 变量在客户端,JSP 变量在服务器端,因此您无法在 JSP 中访问 javascript 变量。但是您可以将需要的数据存储在隐藏字段中,在客户端设置其值并通过 GET 或 POST 在服务器上获取它。
Client side:
客户端:
<script type="text/javascript">
var el = document.getElementById("data");
el.value = "Needed_value";
</script>
<form action="./Your_JSP.jsp" method="POST">
<input id="data" type="hidden" value="" />
<input type="submit" />
</form>
server side:
服务器端:
<%
if (request.getParameter("data") != null) { %>
Your value: <%=request.getParameter("data")%>
<%
}
%>
回答by ram
function call()
{
var name="xyz";
window.location.replace=("a.jsp?m="+name);
}
String name=request.getParameter("name");
if(name!=null){
out.println(name);
}

