在 JSP 中将 Javascript 值传递给 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20677707/
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
Pass Javascript Value to Java in JSP
提问by Mark Karan
I would like to pass a Javascript value to a Java function in JSP. How can I do that? The id comes from a combobox in JSP via Javascript. I will get the ID from ComboBox and send it to Java function as parameter to get the Java result.
我想将 Javascript 值传递给 JSP 中的 Java 函数。我怎样才能做到这一点?id 通过 Javascript 来自 JSP 中的组合框。我将从 ComboBox 获取 ID 并将其作为参数发送到 Java 函数以获取 Java 结果。
function Display()
{
var IdFromCB = (document.getElementById("MListSelect")).value;
//CALL JAVA FUNCTION HERE BY USING IdFromCB as function parameter
//'<% getSomething(-----IdFromCB-----);%>'
}
Thanks,Mark
谢谢,马克
采纳答案by Suresh Atta
You cannot make a server call directly. You need to make a server request.
您不能直接进行服务器调用。您需要发出服务器请求。
javascriptplays on client side and JSPplays on server side.
javascript在客户端播放,JSP在服务器端播放。
What you need is you have to make a server request. And send that string as a query parameter.
您需要的是您必须发出服务器请求。并将该字符串作为查询参数发送。
Two options to achieve this.
实现这一目标的两种选择。
Do not confuse that JSP and java script existed on same document(or file). Yes but JSP part compiles on server side and JavaScript executes by browser.
不要混淆 JSP 和 java 脚本存在于同一个文档(或文件)中。是的,但 JSP 部分在服务器端编译,JavaScript 由浏览器执行。
回答by Satheesh Cheveri
Javascript statements are rendered by Browsers and executed as client program.
Javascript 语句由浏览器呈现并作为客户端程序执行。
In your case if you want execute a java code based on selection of html component, you would need to use ajax
call .
在您的情况下,如果您想根据 html 组件的选择执行 java 代码,则需要使用ajax
call 。
You can find quick example here
您可以在此处找到快速示例
A Simple AJAX with JSP example
http://www.programming-free.com/2013/03/ajax-fetch-data-from-database-in-jsp.html
http://www.programming-free.com/2013/03/ajax-fetch-data-from-database-in-jsp.html