Anton Sementsov
Anton Sementsov

Reputation: 1246

Send GET request to Servlet by link

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/>

Upvotes: 2

Views: 2773

Answers (1)

BalusC
BalusC

Reputation: 1108632

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:

Upvotes: 4

Related Questions