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

标签 java html jsp servlets jstl

<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/

相关文章:

javascript - 表格分页

java - 是否可以使用 Java 类将字符串从 Java 传递到 JSP?

java - Java如何在多态中确定运行时调用的方法?

java - WEB-INF内容访问又一场 war

Java 多个映射检查是否包含

java - 不幸的是 "app"已经停止了 Android Studio 的问题

html - 我如何将一个元素层叠在另一个元素之下?

jquery 最近添加了突出显示的 css 在警报生成后无法删除

mysql - 在 Tomcat 服务器上使用 mySQL?

java - jsp从servlet读取文件名不可能吗?