Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2862
  • Last Modified:

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

0
techques
Asked:
techques
  • 2
1 Solution
 
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
 
mahomeCommented:
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now