简体   繁体   English

在JavaScript和嵌入式Java之间传递变量

[英]Passing a variable between javascript and embedded java

I'm running a web service and calling a method by embedding the java inside the script: 我正在运行Web服务并通过将Java嵌入脚本中来调用方法:

    <script type="text/javascript">
        function getSS(){
<%-- start web service invocation --%>

var x = <%
try {
    org.me.testbilling.TestBillingWS_Service service = new
    org.me.testbilling.TestBillingWS_Service();
    org.me.testbilling.TestBillingWS port = service.getTestBillingWSPort();

    java.lang.String name = "Mike" ;

    java.lang.String result = port.getSS(name);
    out.println(result);
} catch (Exception ex) {
   out.println(ex);
}
%>
<%-- end web service invocation --%>

    alert(x);
        }
    </script>

The issue is that I want this line: 问题是我想要这一行:

java.lang.String name = "Mike";

To be dynamic, so that it can pull a variable from text the user entered, or a selection like this: 为了具有动态性,它可以从用户输入的文本或类似的选择中提取变量:

<select id="getName" onchange="getSS($('#getName').find(":selected").text())">
<option>Mike</option>
<option>John</option>
<option>Patty</option>
<option>Stephanie</option>
</select>

So what I need is either a better way to make that call, or a way to pass the variable dynamically into that script. 因此,我需要的是发出调用的更好方法,或者是将变量动态传递到该脚本中的方法。 Any suggestions? 有什么建议么?

JavaScript is on client machine, but your java is on server. JavaScript在客户端计算机上,但是Java在服务器上。

If you want to send data from client to server, you need to use jQuery.ajax to send. 如果要将数据从客户端发送到服务器,则需要使用jQuery.ajax进行发送。

See this link http://api.jquery.com/jquery.ajax/ 看到此链接http://api.jquery.com/jquery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM