<form action="PetTransactionAction" method="post">
<table>
<tr>
<th>#</th>
<th>Pet Name</th>
<th>Quantity Available</th>
<th>Place</th>
<th>Age</th>
<th>Pet Type</th>
</tr>
<c:forEach items="${petsearch}" var="nm">
<tr>
<td>${nm.id}</td>
<td>${nm.pet_name}</td>
<td>${nm.avl_qty}</td>
<td>${nm.place}</td>
<td>${nm.pet_age}</td>
<td>${nm.pet_type}</td>
<td><input type="hidden" name="hidden" value="${nm.id}"></td>
<td><input type="submit" name="submit" value="Buy"></td>
</tr>
</c:forEach>
</table>
</form>
如何在提交表单后从servlet中的隐藏字段中获取特定值。我正在使用request.getParameter(&#34; hidden&#34;);在servlet中,它只给出第一条记录的id,我如何获得第3或第4条或任何其他记录的值?
答案 0 :(得分:0)
使用String[] values=request.getParameterValues("hidden");
将返回所有参数的数组,其名称为&#34; hidden&#34;。
由于你需要你点击的行的隐藏参数,你应该有多个表格
<table>
<tr>
<th>#</th>
<th>Pet Name</th>
<th>Quantity Available</th>
<th>Place</th>
<th>Age</th>
<th>Pet Type</th>
</tr>
<c:forEach items="${petsearch}" var="nm">
<tr>
<td>${nm.id}</td>
<td>${nm.pet_name}</td>
<td>${nm.avl_qty}</td>
<td>${nm.place}</td>
<td>${nm.pet_age}</td>
<td>${nm.pet_type}</td>
<td colspan='2'>
<form action="PetTransactionAction" method="post">
<input type="hidden" name="hidden" value="${nm.id}">
<input type="submit" name="submit" value="Buy">
</form>
</td>
</tr>
</c:forEach>
</table>
答案 1 :(得分:0)
在您的Servlet中,您可以根据请求使用getParameter api,例如:
String id = httpServletRequest.getParameter("hidden");//hidden is the name of your hidden field i.e. name="hidden"
此方法将请求参数的值作为String返回,如果参数不存在,则返回null