单击图像按钮进入Servlet无需输入任何值

时间:2013-12-19 14:24:29

标签: jsp servlets button dao

我的目标是单击图像按钮时,它将首先通过servlet然后通过数据库对象从数据库中检索数据,结果将显示在下一个jsp页面中。

通常当我需要显示数据库中的数据时,我会要求用户先输入值,然后调出servlet从数据库中检索数据,然后请求调度员到下一页。

但是,现在我希望用户单击图像而不是输入任何值。它可以转到second.jsp但不能显示数据。

我的代码是否有问题..或点击无法访问servlet时的图像?

帮助将不胜感激。谢谢!

以下是我的代码......:)

web.xml

 <servlet>
<display-name>PendingOrderServlet</display-name>
<servlet-name>PendingOrderServlet</servlet-name>
<servlet-class>servlet.PendingOrderServlet</servlet-class>
 </servlet>
   <servlet-mapping>
    <servlet-name>PendingOrderServlet</servlet-name>
   <url-pattern>/PendingOrderServlet</url-pattern>
  </servlet-mapping>

first.jsp

<SCRIPT type="text/javascript">
function doNextPage {
location.href = "PendingOrderServlet";}
</SCRIPT>
<img src="images/image.png" onclick="doNextPage();">

PendingOrderServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
        PendingOrderDAO pod = new PendingOrderDAO();
        request.setAttribute("order", pod.getPendingOrders());

        RequestDispatcher view = request
                .getRequestDispatcher("second.jsp");
        view.forward(request, response);
}

PendingOrderDAO

public ArrayList getPendingOrders() {
    ArrayList<OrderBean> listpendingorder = new ArrayList<OrderBean>();
    try {
        currentCon = ConnectionManager.getConnection();
        Statement statement = currentCon.createStatement();

        ResultSet rs = statement
                .executeQuery("SELECT fborders.fbOrderId, fbmenu.fbMenuName, fborders.quantity, fborders.date, fborders.time, customer.custNRIC FROM fborders INNER JOIN fbmenu ON fborders.fbMenuId=fbmenu.fbMenuId INNER JOIN customer ON customer.custId=fborders.custId WHERE fborders.status='pending'");
        OrderBean OrderBean = null;
        while (rs.next()) {
            OrderBean = new OrderBean();
            OrderBean.setFbOrderId(rs.getInt("fbOrderId"));
            OrderBean.setFbMenuName(rs.getString("fbMenuName"));
            OrderBean.setQuantity(rs.getString("quantity"));
            OrderBean.setDate(rs.getString("date"));
            OrderBean.setTime(rs.getString("time"));
            OrderBean.setCustNRIC(rs.getString("custNRIC"));
            listpendingorder.add(OrderBean);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return listpendingorder;
}

second.jsp

    <table class="table tableshow">
                    <thead>
                        <tr>
                            <th>ffOrder ID</th>
                            <th>Menu</th>
                            <th>Quantity</th>
                            <th>Date</th>
                            <th>Time</th>
                            <th>Customer NRIC</th>
                            <th>.....</th>
                        </tr>
                    </thead>

                    <tbody>
                            <c:forEach items="${pendingorder}" var="order">
                    <tr>
                        <td>${pendingorder.getFbOrderId()}</td>
                        <td>${pendingorder.getFbMenuName()}</td>
                        <td>${pendingorder.getQuantity()}</td>
                        <td>${pendingorder.getDate()}</td>
                        <td>${pendingorder.getTime()}</td>
                        <td>${pendingorder.getCustNRIC()}</td>

                    </tr>
                </c:forEach>

                    </tbody>

                </table>

1 个答案:

答案 0 :(得分:1)

在Servlet中。您传递了名为&#39; order&#39;但在JSP中,您使用了名为&#39; pendingorder&#39;。

的属性

Servlet代码:

request.setAttribute("order", pod.getPendingOrders());

正确的JSP:

<c:forEach items="${order}" var="item">
    <tr>
                    <td>${item.getFbOrderId()}</td>
                    <td>${item.getFbMenuName()}</td>
                    <td>${item.getQuantity()}</td>
                    <td>${item.getDate()}</td>
                    <td>${item.getTime()}</td>
                    <td>${item.getCustNRIC()}</td>

    </tr>
</c:forEach>

注意:您可以使用$ {item.fbOrderId}而不是$ {item.getFbOrderId()}