gpt4 book ai didi

java - 将数据从 JSP 传递到 Servlet,在 Servlet 中返回 null

转载 作者:行者123 更新时间:2023-12-01 22:47:01 27 4
gpt4 key购买 nike

<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>&#8377;<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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com