Call a servlet on click of hyperlink

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

The servlet-mapping element defines a mapping between a servlet and a URL pattern. The example below maps the servlet named myservlet to any URL that starts with /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="https://stackoverflow.com/foo/test.html">Click Me</a> would invoke the servlet.

Leave a Comment