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

Posted on 2009-02-12
Last Modified: 2013-11-24

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

Question by:techques
    LVL 10

    Expert Comment

    Just call your servlet like in the web.xml declared

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

    Open in new window


    Author Comment

    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?
    LVL 10

    Accepted Solution

    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.
    LVL 19

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    scoresClump  challenge 31 85
    countPairs challenge 7 34
    strDist challenge 35 69
    word0 challenge 3 26
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now