Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a servlet on click of hyperlink

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

like image 850
sarah Avatar asked Dec 15 '09 13:12

sarah


2 Answers

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="/foo/test.html">Click Me</a> would invoke the servlet.
like image 121
John Topley Avatar answered Sep 21 '22 18:09

John Topley


  1. you declare your servlet in web.xml by 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
like image 41
Bozho Avatar answered Sep 19 '22 18:09

Bozho