Java 单击超链接调用 servlet

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

Call a servlet on click of hyperlink

javaservletsjakarta-ee

提问by sarah

Is there a way to call a Java Servlet on click of hyperlink without using JavaScript?

有没有办法在不使用 JavaScript 的情况下单击超链接来调用 Java Servlet?

采纳答案by John Topley

Make the hyperlink have a URL that you have a servlet mapping defined for in the web.xmlfile.

使超链接具有您在web.xml文件中为其定义的 servlet 映射的 URL 。

The servlet-mappingelement defines a mapping between a servlet and a URL pattern. The example below maps the servlet named myservletto any URL that starts with /foo:

servlet-mapping元素定义了 servlet 和 URL 模式之间的映射。下面的示例将命名的 servlet 映射myservlet到任何以 开头的 URL /foo

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>com.stackoverflow.examples.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>/foo/*</url-pattern>
</servlet-mapping>
  • For this example, a hyperlink such as <a href="/foo/test.html">Click Me</a>would invoke the servlet.
  • 对于此示例,诸如<a href="/foo/test.html">Click Me</a>将调用 servlet的超链接。

回答by Warrior

Think that you've defined a servlet "callme" and web.xml has been configured for this servlet. Use the following syntax to call it using hyperlink

假设您已经定义了一个 servlet“callme”,并且已经为这个 servlet 配置了 web.xml。使用以下语法使用超链接调用它

web.xml

网页.xml

<servlet>
<description>callme Functions</description>
<display-name>callme</display-name>
<servlet-name>callme</servlet-name> <servlet-class>com.test.Projects.callme</servlet- 
class>
</servlet>

<servlet-mapping>
<servlet-name>callme</servlet-name>
<url-pattern>/callme</url-pattern>
</servlet-mapping>

in JSP:

在 JSP 中:

<a href="<%=request.getContextPath()%>/callme">Call the servlet</a>

回答by Bozho

  1. you declare your servlet in web.xmlby setting its name, class and url-pattern(let's say your url-pattern is /myServlet)
  2. write <a href="/myServlet">mylink</a>
  3. override the doGet(..)method of the servlet to do whatever you want
  1. web.xml通过设置其名称、类和url-pattern 来声明您的 servlet (假设您的 url-pattern 是/myServlet
  2. <a href="/myServlet">mylink</a>
  3. 覆盖doGet(..)servlet的方法来做任何你想做的事

回答by Michael Borgwardt

What exactly do you mean with "call a Java Servlet? The most normal (i.e. without any JavaScript magic) browser behaviour for clicking on a link is to send a HTTP request to fetch the document at the URL specified in the link and display it - and Servlets exist to respond to HTTP requests.

“调用 Java Servlet”究竟是什么意思?点击链接的最正常(即没有任何 JavaScript 魔法)浏览器行为是发送 HTTP 请求以在链接中指定的 URL 处获取文档并显示它 -和 Servlet 的存在是为了响应 HTTP 请求。

So you don't have to do anything special at all. Just have a regular HTML link and make sure that the servlet you want to "call" corresponds to that link's URL. Of course the next question is what that Servlet returns and what you want the browser to do with it.

所以你根本不需要做任何特别的事情。只需有一个常规的 HTML 链接,并确保您要“调用”的 servlet 对应于该链接的 URL。当然,下一个问题是 Servlet 返回什么以及您希望浏览器如何处理它。