如何在 JSP 的 Java 代码中使用 Javascript 变量作为参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26940739/
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 use Javascript variable as argument in java code in JSP?
提问by Salil Chauhan
I want to send code title and price as an argument to the book object but gives illegal start expression error.
我想将代码标题和价格作为参数发送给 book 对象,但给出了非法开始表达式错误。
function addrecord() {
code = document.getElementById('code').value;
title = document.getElementById('title').value;
price = parseFloat(document.getElementById('price').value);
<% Book book = new Book(%> code <% ,%> title <% , %> price <% );
bookdb.addRecord(book);
%>
document.getElementById("code").value = "";
document.getElementById("title").value = "";
document.getElementById("price").value = "";
}
回答by Joeblade
You are misunderstanding where java and javascript run. You use <% and %> as if that sends values to the server, which it does not. those tags are used only inside the jsp to start snippets of java code. This only works while you are generating the page. Once your html is at your browser, these tags will not do anything.
您误解了 java 和 javascript 的运行位置。您使用 <% 和 %> 就好像将值发送到服务器一样,但它没有。这些标签仅在 jsp 内部用于启动 java 代码片段。这仅在您生成页面时有效。一旦你的 html 出现在你的浏览器中,这些标签就不会做任何事情。
Highlighting important keywords, this is not intended as sarcarsm
突出重要关键字,这不是讽刺
When you request a jsp page with your browser, the browseropens a network connection to the server. Think of these 2 (browser, server) as living in 2 completely different places.
当你请求一个jsp页面与您的浏览器,该浏览器打开一个网络连接到服务器。将这 2 个(浏览器、服务器)视为生活在 2 个完全不同的地方。
The serverthen runs the java code. When it executes, it generates an html page. This html pageis sent back across the network connection to the client (your browser)
然后服务器运行 java 代码。当它执行时,它会生成一个html 页面。此html 页面通过网络连接发送回客户端(您的浏览器)
Your browserwill receive this fully formed html page and use it to create it's view. On this page you can run javascript but the javascript will only run on the client (browser)
您的浏览器将收到这个完整的 html 页面并使用它来创建它的视图。在此页面上,您可以运行 javascript,但 javascript 只能在客户端(浏览器)上运行
To get any variable back to the serveragain, you will need to either: get or post a form (using a submit inside a form tage, or use JavaScript post request like a form submit) , do an ajax call (http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml) or have the user click a link (a href="...your page").
要将任何变量再次返回到服务器,您需要:获取或发布表单(使用表单标签内的提交,或使用JavaScript 发布请求,如表单提交),执行 ajax 调用(http:// www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml)或让用户点击一个链接(a href="...your page")。
The easiest way is to do:
最简单的方法是:
<form method="POST" action="your/page/url">
<input type="hidden" name="someName" value="someValue" />
<input type="submit" />
</form>
read some of the links and make sure you keep in mind, your browser and the server are separated, so running javascript in your browser will not reach the server.
阅读一些链接并确保您记住,您的浏览器和服务器是分开的,因此在您的浏览器中运行 javascript 不会到达服务器。
回答by ash
See here: Assign JavaScript variable to Java Variable in JSP
请参阅此处:将 JavaScript 变量分配给 JSP 中的 Java 变量
As that states, it's really not possible as the JSP executes on the server, and the javascript executes later in the browser.
如前所述,这真的不可能,因为 JSP 在服务器上执行,而 javascript 稍后在浏览器中执行。