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

时间:2014-08-08 11:33:41

标签: java html5 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);
}

1 个答案:

答案 0 :(得分:3)

您使用的location.href='passtopayment'不是提交表单的正确方法。

它就像对Servlet的单独请求一样,不会发送给Servlet。

您应该使用form并将请求提交给Servlet。

<form action="passtopayment" method="post">
       <!-- HTML controls -->
       <input type="submit" value="Submit"/>
</form>

以下是详细Example