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

Hi

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 s.id as id, s.name as name FROM customer s";
ResultSet rs = s.executeQuery(sql);
%>
<% while (rs.next()) {%>
<tr><td>
<%=rs.getInt("id")%></td><td><%=rs.getString("name")%></td><td><a href="test.jsp?id=<%=rs.getInt("id")%>">click me</a></td></tr>
<%}%>
<%
rs.close();
s.close();
con.close();
%>

Open in new window

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

http://localhost:8080/mycontext/myservlet?id=123

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

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

<a href="/myservlet?id=<%=rs.getInt("id")%>
 
web.xml
-------
  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>test.MyServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/myservlet</url-pattern>
  </servlet-mapping>

Open in new window

0
 
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?
0
 
KuldeepchaturvediCommented:
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.
0
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.