Http Session计算

时间:2014-12-13 19:28:10

标签: java servlets

我正在制作一个shopping网络应用程序,我有一个第一个html表单,在这里:

<!DOCTYPE html>
<html>
<head>
    <META HTTP-EQUIV="Pragma" CONTENT="no cache">
    <META HTTP-EQUIV="Cache-control"
          CONTENT="no cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <TITLE>Shopping Cart</TITLE>
    <title> Shopping Cart </title>
</head>
<body>
<center>
    <h3> Simple Shopping Cart </h3>

    <form method=post action="Selection">

        <table>
            <tr>
                <td>
                    <input type="radio" name="Product" value="Apples" checked>
                    <font color=blue> Apples </font>
                </td>
            </tr>

            <tr>
                <td>
                    <input type="radio" name="Product" value="Pears">
                    <font color=blue> Pears </font>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="radio" name="Product" value="Checkout">
                    <font color=blue> Go To the checkout </p> </font>
                </td>
            </tr>
        </table>
        <BR><BR><BR>
        <input type="submit" value="Submit">
    </form>
</center>
</body>
</html>

这是我的Selection Servlet:

@WebServlet("/Selection")
public class Selection extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String currentProduct = request.getParameter("Product");

    HttpSession cart = request.getSession();
    cart.setAttribute("currentProd", currentProduct);

    if (currentProduct.equals("Checkout")) {
        response.sendRedirect("Checkout"); // Send to Checkout Servlet
    } else {
        sendPage(response, currentProduct); //Creates page for selection of weight.
    }
}

private void sendPage(HttpServletResponse reply, String product) throws IOException {
    reply.setContentType("text/html");
    PrintWriter out = reply.getWriter();
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>" + product + "</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");
    out.println("<CENTER>");
    out.println("<h1><font color=blue>" + product + " </font> </h1>  ");
    out.println("<BR><BR><BR>");
    out.println(" <form method=post action='Weight' ");
    out.println("<TABLE>");
    out.println("<TR>");
    out.println(" <TD>Quantity required (kg)");
    out.println(" <INPUT TYPE='Text' NAME='Qty' VALUE='' SIZE=5></TD>");
    out.println("</TR>");
    out.println("</TABLE>");
    out.println("<BR><BR><BR>");

    out.println("<TABLE>");
    out.println("<TR>");
    out.println(" <TD> <INPUT TYPE = 'radio' NAME='Option' VALUE='Add' Checked> ");
    out.println(" <FONT COLOR=blue> "
            + "Add to cart.</FONT></TD>");
    out.println("</TR>");
    out.println("<TR>");
    out.println(" <TD><INPUT TYPE='Radio'"
            + " NAME='Option' VALUE='Remove'>");
    out.println(" <FONT COLOR=blue> " +
            "Remove item from cart.</FONT></TD>");
    out.println("</TR>");
    out.println("<TR>");
    out.println(" <TD><INPUT TYPE='Radio'"
            + " NAME='Option' VALUE='Next'>");
    out.println(" <FONT COLOR=blue> "
            + "Choose next item.</FONT></TD>");
    out.println("</TR>");
    out.println("<TR>");
    out.println(" <TD><INPUT TYPE='Radio'"
            + " NAME='Option' VALUE='Checkout'>");
    out.println(" <FONT COLOR=blue> "
            + "Go to checkout.</FONT></TD>");
    out.println("</TR>");
    out.println("</TABLE>");
    out.println("<BR><BR><BR>");
    out.println("<INPUT TYPE='Submit' VALUE='Submit'>");

    out.println("</FORM>");
    out.println("</CENTER>");
    out.println("</BODY>");
    out.println("</HTML>");
    out.flush();

}
}

最后,这是Checkout Servlet:

@WebServlet("/Checkout")
public class Checkout extends HttpServlet {
    private final float APPLES_PRICE = 1.45F;
    private final float PEARS_PRICE = 1.75F;

    //In a real application, the above prices would be
//retrieved from a database, of course.
    public void service(HttpServletRequest request,
                        HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession cart = request.getSession();
        response.setContentType("text/HTML");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>Checkout</TITLE>");
        out.println("</STYLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<BR><BR><BR>");
        out.println("<CENTER>");
        out.println(
                "<H1><FONT COLOR=Blue>Order List</FONT></H1>");
        out.println("<BR><BR><BR>");
        out.println("<TABLE BGCOLOR=Aqua BORDER=2>");
        out.println("<TR>");
        out.println("<TH>Item</TH>");
        out.println("<TH>Weight(kg)</TH>");
        out.println("<TH>Cost(£)</TH>");
        out.println("</TR>");
        cart.removeAttribute("currentProd");

        Enumeration prodNames = cart.getAttributeNames();
        float totalCost = 0;
        int numProducts = 0;
        while (prodNames.hasMoreElements()) {
            float wt = 0, cost = 0;
            String product = (String) prodNames.nextElement();
            String stringWT = (String) cart.getAttribute(product);
            wt = Float.parseFloat(stringWT);

            if (prodNames.equals("Apples")) {
                cost = APPLES_PRICE * wt;
            } else if (prodNames.equals("Pears")) {
                cost = PEARS_PRICE * wt;
            }
            out.println("<TR>");
            out.println("<TD>" + product + "</TD>");
            out.format("<TD> %4.2f </TD>%n", wt);
            out.format("<TD> %5.2f </TD>%n", cost);
            out.println("</TR>");
            totalCost += cost;
            numProducts++;
        }
        if (numProducts == 0) {
            out.println("<TD>*** No orders placed! ***</TD></TR>");
        } else {
            out.println("<TD></TD>"); //Blank cell.
            out.println("<TD>Total cost:</TD>");
            out.format("<TD> %5.2f </TD>%n", totalCost);
            out.println("</TR>");
        }
        out.println("</TABLE>");
        out.println("</CENTER>");
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
    }
    }

我的问题在于计算cost和总费用变量。

在结果中,每个购物结帐都已分配到Zero

enter image description here

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

ProdNames它是您所选产品的枚举,您想要将"Apple"与当前产品进行比较。

替换

 if (prodNames.equals("Apples")) { // never true
     cost = APPLES_PRICE * wt;
 } else if (prodNames.equals("Pears")) { // never true
     cost = PEARS_PRICE * wt;
 } 

with:

 if (product.equals("Apples")) {
     cost = APPLES_PRICE * wt;
 } else if (product.equals("Pears")) {
     cost = PEARS_PRICE * wt;
 }

顺便说一句,您应该看看如何调试您的应用程序。它将帮助您管理此类问题。