从动态生成的表单调用servlet

时间:2014-07-26 03:54:27

标签: java html jsp servlets

我正在模拟在线商店上学。我将我的产品存储在SQL数据库中。我想在数据库中显示所有产品,旁边有按钮,将其ID发送到cart_servlet。为了显示我正在生成如下表格的产品:

public static String getInventory(){
    String result = "<table>";
    for ( Product p : DAO_Product.getProducts() ) {
        result = result + "<tr>"
                + "<td>" + p.getName() + "</td>"
                + "<td><img src=\"resources/images/" + p.getImage() + "\" alt=\"Duke waving his hand\"></td>"
                + "<td>" + p.getDollars() + "</td>"
                + "<td>" + p.getPennies() + "</td>"
                + "<td>" + p.getStock() + "</td>"
                + "<td>" + p.getDescription() + "</td>"

                //creates a form consisting of one button and a hidden value
                //clicking the button should submit the corresponding hidden value
                + "<td><form action=\"${pageContext.request.contextPath}/cart_servlet\" method=\"post\">"
                + "<input type=\"hidden\" name=\"product\" value=\"" + p.getId() + "\" />"
                + "<input type=\"submit\" name=\"submit\" value=\"Add to Cart\" />"
                + "</form>"
                //End Form

                + "</tr>";
    }
}

这会创建我想象的正确表格,并且它生成的HTML看起来是正确的:

output form

<table><tr><td>Caverna</td>
    <td><img src="resources/images/caverna.jpg" alt="sampleImage"></td>
    <td>112</td><td>12</td><td>34</td>
    <td> you are the bearded leader of a small dwarf family which lives in a little cave in the mountains. Together, you</td>

<td><form action ="${pageContext.request.contextPath}/cart_servlet" method="post">
    <input type="hidden" name="product" value="4" />
    <input type="submit" name="submit" value="Add to Cart" />
</form>

但是当我点击按钮时,我会看到:'HTTP状态404 - 未找到'描述:'请求的资源不可用'

我要求的Servlet是我的'CartServlet':

@WebServlet( "/cart_servlet" )
public class CartServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String result;
    Status status = new Status();
    req.setAttribute( "status", status );
    //RequestDispatcher view = req.getRequestDispatcher( "browseProducts.jsp" );
    if ( req.getParameter( "submit" ) != null ) {
    }
    //view.forward( req, resp );
    }
}

我已经在我的'CartServlet'中评论了所有内容,我确信它可能无法以某种方式获取上下文或找到该类,但我不确定如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

事实证明我接近这一切都错了。相反,我应该使用JSTL在我的JSP中构建表。使用以下代码,我可以删除'getInventory()':

第一步是将以下行添加到JSP的顶部:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

注意确保已下载JSTL库并将其添加到项目中。你可以找到它here

第二步是声明您需要的任何变量。在这种情况下,我需要一个'Products'的ArrayList:

<%
    ArrayList<Product> products = DAO_Product.getProducts();
    //Set an attribute to reference our Arraylist
    pageContext.setAttribute( "products", products );
 %>

现在我们已经准备好构建我们的表了。该表将显示有关每个产品的信息,并在最后一列中有一个名为“cart_servlet”的按钮。我们将使用'JSTL'中的'forEach':

<table border="1" cellpadding="5" >
    <tr>
        <th>Name</th>
        <th>Image</th>
        <th>Price</th>
        <th>Description</th>
        <th>Stock</th>
        <th>Add to Cart?</th>
    </tr>
    <c:forEach var="p" items="${products}" >
        <tr>
            <td>${p.name}</td>
            <td><img src="resources/images/${p.getImage()}" alt="Cool Pic"></td>
            <td>$${p.dollars}.${p.pennies}</td>
            <td>${p.description}</td>
            <td>${p.stock}</td>
            <td>
                <form action="${pageContext.request.contextPath}/cart_servlet" method="post">
                    <input type="hidden" name="productID" value="${p.id}">
                    <input type="submit" name="addToCart" value="Add to Cart">
                </form>
            </td>
        </tr>
    </c:forEach>
</table>