<table border="1" cellpadding="5" id="newtable">
<!-- <caption id="tablehead">Rooms are available!</caption> -->
<!-- <tr class="hover"> -->
<tr>
<th>Room No</th>
<th>AC</th>
<th>Deluxe</th>
<th>Tariff</th>
</tr>
<c:forEach var="room" items="${myrooms}">
<tr bgcolor="#4B476F" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#4B476F';">
<td class="nr">1</td>
<td name="ac"><c:out value="${room.ac}" /></td>
<td name="deluxe"><c:out value="${room.deluxe}" /></td>
<td>₹<c:out value="${room.price}" /></td>
<td><button type="button" class="mybutton" onclick="location.href='passtopayment'">Pay</button> </td>
</tr>
</c:forEach>
</table>
我想在单击相应行时获取 AC 和 Deluxe 列的 td 值。但是,当执行我得到的以下 servlet 代码时,会打印 null null 。请帮忙!
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ac = request.getParameter("ac");
String deluxe = request.getParameter("deluxe");
out.println(ac);
out.println(deluxe);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
最佳答案
您使用的location.href='passto payment'
不是提交表单的正确方式。
这就像对 Servlet 的单独请求,不会向 Servlet 发送任何内容。
您应该使用form
并将请求提交给Servlet。
<form action="passtopayment" method="post">
<!-- HTML controls -->
<input type="submit" value="Submit"/>
</form>
这里有详细的Example
关于java - 将数据从 JSP 传递到 Servlet,在 Servlet 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25202581/