在javascript中调用函数,而在while中必须调用java函数
我正在创建一个带有两个文本框的页面。当用户在框中输入值,然后单击提交按钮时,我会调用一个函数。在该函数中,我得到了这两个值。然后我必须将这些值传递给一个将返回结果的java函数。然后我将显示该结果。我不知道该怎么做。我已经写了代码。但它最终会出错。
<%@ page import="java.util.*,mypackage.JavaFile"%>
<html>
<HEAD>
<TITLE>
sample script
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function getResult(form){
var value1 = form.val.value;
var value2 = from.val1.value;
<%
String res = JavaFile.myFunc(value1,value2);
%>
return res;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="form" ACTION="" METHOD="GET">Enter value1:<br>
<INPUT TYPE="text" NAME="val" VALUE=""><P>
Enter value2<br>
<INPUT TYPE="text" NAME="val1" VALUE=""><P>
<INPUT TYPE="button" value="submit" onClick="getResult(this.form)">
</FORM>
</BODY>
</HTML>
编译错误 value1 value2 未解决。请帮忙。
I'm creating one page with two text boxes. When user enters the value in the box and then onclick submit button i make call to the one function. In that function, i'm getting those two values. Then i have to pass those values to one java function which will return result. Then i'll display that result. i dont know how to do this. I have written code. But it end up with error.
<%@ page import="java.util.*,mypackage.JavaFile"%>
<html>
<HEAD>
<TITLE>
sample script
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function getResult(form){
var value1 = form.val.value;
var value2 = from.val1.value;
<%
String res = JavaFile.myFunc(value1,value2);
%>
return res;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="form" ACTION="" METHOD="GET">Enter value1:<br>
<INPUT TYPE="text" NAME="val" VALUE=""><P>
Enter value2<br>
<INPUT TYPE="text" NAME="val1" VALUE=""><P>
<INPUT TYPE="button" value="submit" onClick="getResult(this.form)">
</FORM>
</BODY>
</HTML>
In compilation error value1 value2 are not resolved. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以你会因为 JSP 是服务器端代码而 javascript 是浏览器端代码这一事实而感到困惑。您实际上需要通过 HTTP POST 提交表单,并从 POST 请求正文中获取 value1 和 value2 值,然后在 JSP java 代码中使用它们。
So you are getting confused by the fact that you JSP is server side code but javascript is browser side code. You need to actually submit the form via an HTTP POST and get the value1 and value2 values from the POST request body, then use them in your JSP java code.
您将 java 与 javascript 混合在一起 - javascript 代码在客户端(即浏览器)上运行,而 java 代码在您的服务器上执行。不同的计算机,不同的编程语言,完全不同的环境。
您必须将带有(ajax?)命中的变量发送到服务器,调用您的java函数,返回响应,浏览器捕获响应并将其显示给用户
You are mixing java with javascript - javascript code is run on the client side (ie: the browser) and java code is executed on your server. Different computers, different programming languages, totally different contexts.
You have to send the variables with a (ajax?) hit to the server, call your java function, return the response and the browser to capture the response and show it to the user
因此,您需要区分客户端 JavaScript 和服务器端 Java。如果您为此创建两个单独的页面会更简单。在第一页上,您可以创建带有输入和提交按钮的表单。让该表单提交到第二页。
在第二页上,您可以尝试获取从客户端提交的值。在这种情况下根本不需要使用 JavaScript。
要获取从 JSP 页面上的 Java 表单提交的值,您可以:
现在您可以在您希望调用的 Java 方法中使用这些值。
So you need to distinguish between client side JavaScript and server side Java. It would be simpler if you created two separate pages for this. On the first page you can create the form with its inputs and submit button. Let that form submit to a second page.
On the second page you can then attempt to get the values that were submitted from the client. There is no need to use JavaScript at all in this scenario.
To get the values submitted from the form in Java on a JSP page you can:
Now you can use the values in your Java method that you wish to call.