如何从 servlet 调用 JavaScript 的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10258425/
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 to call function of JavaScript from servlet
提问by Sunil Kumar Sahoo
I am new to web development. I have an external JavaScript file which has a function to show a prompt with error details. I need to pass the error message to the function. I have written contoller in servlet.
我是网络开发的新手。我有一个外部 JavaScript 文件,它具有显示错误详细信息提示的功能。我需要将错误消息传递给函数。我在 servlet 中编写了控制器。
How to call the function of that JavaScript file from my servlet.
如何从我的 servlet 调用该 JavaScript 文件的函数。
采纳答案by Ravindra Gullapalli
It is not possible to call a java script function from a servlet. Rather, you can print javascript code using
无法从 servlet 调用 java 脚本函数。相反,您可以使用打印 javascript 代码
response.getOutputStream().println("[javascript code]");
response.getOutputStream().println("[javascript code]");
into the browser and then the javascript function will be executed in the browser.
进入浏览器,然后javascript函数将在浏览器中执行。
回答by Ved
You can achieve similar kind of behavior by using following method. While sending response itself you can provide JS events.
您可以使用以下方法实现类似的行为。在发送响应本身时,您可以提供 JS 事件。
PrintWriter out = response.getWriter();
out.println("<tr><td><input type='button' name='Button' value='Search' onclick=\"searchRecord('"+ argument + "');\"></td></tr>");
So when you click on Search button, search record method of JS will be called.
所以当你点击 Search 按钮时,就会调用 JS 的搜索记录方法。
回答by BalusC
You need to understand that a servlet runs in the webserver, not in the webbrowser and that JS runs in the webbrowser, not in the webserver. The normal practice is to let the servlet forward the request to a JSPfile which in turns produces HTML/CSS/JS code which get sent to the webbrowser by the webserver. Ultimately, all that HTML/CSS/JS code get executed in the webbrowser.
您需要了解 servlet 在 web 服务器中运行,而不是在 webbrowser 中,并且 JS 在 webbrowser 中运行,而不是在 web 服务器中。通常的做法是让 servlet 将请求转发到JSP文件,该文件依次生成 HTML/CSS/JS 代码,这些代码由网络服务器发送到网络浏览器。最终,所有 HTML/CSS/JS 代码都会在网络浏览器中执行。
To achieve your (somewhat strange, tbh) functional requirement, just let the forwarded JSP conditionally render the particular script call. For example as follows with JSTL<c:if>
, assuming that you've collected and set the errors as ${errors}
in JSON array format:
为了实现您的(有点奇怪,tbh)功能需求,只需让转发的 JSP 有条件地呈现特定的脚本调用。例如下面的JSTL<c:if>
,假设您已经${errors}
以 JSON 数组格式收集和设置错误:
<c:if test="${not empty errors}">
<script>displayErrors(errors);</script>
</c:if>
Or let JSP assign it as a JS variable and let JS handle it further. Something like:
或者让JSP把它赋值为一个JS变量,让JS进一步处理。就像是:
<script>
var errors = ${errors};
if (errors.length) {
displayErrors(errors);
}
</script>
As to the requirement being strange, if you're forced to use JS to display messages, that can only mean that you're using an alert()
or something. This is very 90's and not user friendly. Just let JSP produce the HTML accordingly that they're put next to the input fields, or in a list on top of the form. Our servlets wiki pagehas a hello world example which does exactly like that.
至于奇怪的要求,如果你被迫使用JS来显示消息,那只能说明你在使用analert()
或什么的。这是非常 90 年代的,而不是用户友好的。只需让 JSP 相应地生成 HTML,将它们放在输入字段旁边或表单顶部的列表中。我们的 servlets wiki 页面有一个 hello world 示例,它的功能完全如此。