How to click a hyperlink to call a servlet instead of a jsp page?


I have the following jsp to list out rows of data from a table and each row of data has its id. When I click the "click me" link, it will pass a parameter to a jsp.

However, my code to process it is written in a servlet. How can I pass id directly to a servlet when I click the hyperlink?

The servlet code:
String in = request.getParameterValues("id");

String sql = "SELECT as id, as name FROM customer s";
ResultSet rs = s.executeQuery(sql);
<% while ( {%>
<%=rs.getInt("id")%></td><td><%=rs.getString("name")%></td><td><a href="test.jsp?id=<%=rs.getInt("id")%>">click me</a></td></tr>

Open in new window

Who is Participating?
mahomeConnect With a Mentor Commented:
No it has nothing to do with your JSP. Test it directly in your browser:


Check your web.xml:
<servlet-class> hat to be full qualified: for example com.myCompany.servlets.MyServlet

Post a stacktrace.
Just call your servlet like in the web.xml declared

<a href="/myservlet?id=<%=rs.getInt("id")%>

Open in new window

techquesAuthor Commented:
I did add the servlet to web.xml

when I call it, throw error:The requested resource (/myservlet) is not available

Do I need to <%@page import="myservlet",myservlet.*;"%> in the head of the jsp page?
The requested resource (/myservlet) is not available

it will come if your url pattern is different in web.xml

post the section of your web xml where you are specifying your servlet.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.