Issue
I am trying to get a <td>
data which click the button and fetch it to the servlet. I've tried to add a hidden input which used to store the data from ResultSet but I only can get the very first row of the data.
If there are any other better method to do it, please do tell me instead, thank you.
<form action="Order_view" method="post">
<%
try {
//Connection Code
%>
<table border=1 align=center style="text-align: center">
<tbody>
<tr>
<th>Package ID</th>
<th>Type</th>
</tr>
<%while (rs.next()) {
%>
<tr>
<td><%out.print(rs.getString(1));%></td>
<td><%out.print(rs.getString(2));%></td>
<td><button name="btnView">View</button></td>
<input type="hidden" name="id" value="<%=rs.getString(1)%>" />
</tr>
<% } %>
</tbody>
</table>
<br>
<%} catch (SQLException e) {
out.print(e.getMessage());%><br><%
}
%>
</form>
Solution
You don't need <form>
to do that,also yes you will be getting first row only, to overcome that you can do like below :
<%
try {
//Connection Code
%>
<table border=1 align=center style="text-align: center">
<tbody>
<tr>
<th>Package ID</th>
<th>Type</th>
<th>Action</th>
</tr>
<%while (rs.next()) {
%>
<tr>
<td><%out.print(rs.getString(1));%></td>
<td><%out.print(rs.getString(2));%></td>
<!--here we are passing "id" of that particular row in <a href>-->
<td><a href="Yourservleturl?id=<%=resultset.getString(1) %> "><input type="button" value="View"></a></td>
</tr>
<% } %>
</tbody>
</table>
<br>
<%} catch (SQLException e) {
out.print(e.getMessage());%><br><%
}
%>
And then in your servlet
get that id by using request.getParameter("id");
in your doGet
method for futher processing.
Answered By - Swati