2

How can I send data via

<a href="#">link<a/>

to my servlet so that it executes my

protected void doGet()

method?

I want to do something like:

<a href="article?todo=show_article&article_id=23">link<a/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Anton Sementsov
  • 1,196
  • 6
  • 18
  • 34

1 Answers1

3

Just let the link point to an URL which matches the URL pattern of the servlet as configured in web.xml or by @WebServlet annotation. The example link as you have expects the servlet to be mapped on an URL pattern of /article. Its doGet() method (if properly @Overriden) will then be called. The request parameters will then be available the usual way by request.getParameter().

String todo = request.getParameter("todo");
String article_id = request.getParameter("article_id");
// ...

With the link example as you've given, the JSP page containing the link should by itself however be located in the root folder of the web content or forwarded by a request URL whose base is the context root. Otherwise you need to make the link URL domain-relative by prefixing the URL with ${pageContext.request.contextPath} like so:

<a href="${pageContext.request.contextPath}/article?todo=show_article&article_id=23">link</a>

(please note that you've a syntax error in closing tag, I've fixed it in above example)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i put my servlet to others and anotated `@WebServlet("/Article")` put domain relative link to href... it looks like: `http://localhost:8080/masite/article/?todo=add_article` but have HTTP Status 404 - /masite/article/. What im doing wrong? My servlets root is :src/servlets/Article.java – Anton Sementsov Feb 17 '12 at 15:26
  • 1
    URLs are case sensitive. `/Article` is not the same as `/article`. You need to lowercase the `A` in the URL pattern as follows `@WebServlet("/article")`. – BalusC Feb 17 '12 at 15:27