javascript 如何使 HTML 链接调用 servlet 中的 java 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14637710/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:11:02  来源:igfitidea点击:

How do you make a HTML link call a java function within your servlet

javascripthtmljsphyperlink

提问by EMChamp

public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;

public Test() {
    super();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    out.println("<HTML><HEAD><TITLE>MovieDB</TITLE></HEAD>");
    out.println("<BODY><H1>MovieDB</H1>");

    out.println("<a href = '#' onclick = 'on_Click();'> Call Function </a>");

}

public void on_Click()
{
    System.out.println("HELLO");
}
}

I just want the HTML Link on my page to call my java function on_Click(), what is a good way to do this?

我只是想让我页面上的 HTML 链接调用我的 java 函数 on_Click(),有什么好的方法可以做到这一点?

采纳答案by BalusC

The onclickis not Java. It's JavaScript. It's a completely different language than Java. The only things which they've in common are the first 4 characters of the language name, some keywords and a some syntax. But that's it.

onclick不是Java的。它是JavaScript。它是一种与 Java 完全不同的语言。它们唯一的共同点是语言名称的前 4 个字符、一些关键字和一些语法。但就是这样。

Just let the link point to an URL which matches the URL pattern of the servlet mapping. Imagine that you've mapped your servlet on an URL pattern of /foo/*, then just use

只需让链接指向与 servlet 映射的 URL 模式匹配的 URL。想象一下,您已将 servlet 映射到 的 URL 模式/foo/*,然后只需使用

<a href="foo">Call function</a>

This will just call the servlet's doGet()method.

这将只调用 servlet 的doGet()方法。

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("Hello");
}

If you want to reuse the same servlet for multiple actions, just pass some action identifier along as request parameter

如果你想为多个动作重用同一个 servlet,只需传递一些动作标识符作为请求参数

<a href="foo?action=bar">Call function</a>

with in doGet()of servlet

doGet()servlet 中

String action = request.getParameter("action"); // "bar"

or as path info

或作为路径信息

<a href="foo/bar">Call function</a>

with in doGet()of servlet

doGet()servlet 中

String action = request.getPathInfo().substring(1); // "bar"

No need for weird JavaScript approaches/workarounds.

不需要奇怪的 JavaScript 方法/解决方法。

See also:

也可以看看:



Unrelatedto the concrete problem, HTML belongs in JSP, not in Servlet.

具体问题无关,HTML 属于JSP,而不属于Servlet。

回答by hobberwickey

A servlet runs on your server, while HTML runs on the user's computer so you'll need to use javascript/ajax to send a request to your server to execute your on_Click function.

servlet 在您的服务器上运行,而 HTML 在用户的计算机上运行,​​因此您需要使用 javascript/ajax 向您的服务器发送请求以执行您的 on_Click 函数。