使用Servlet的购物车

时间:2014-10-23 15:20:09

标签: java javascript session servlets shopping-cart

我有一个简单的购物车代码,可以在html页面的游戏下拉列表中添加产品。我编辑了html页面,删除了下拉列表,并为每个游戏添加了“添加到购物车”按钮。所以我一直在尝试编辑购物车,只要点击“添加到购物车”按钮就可以添加游戏但不能。请注意,我使用jetty-9 servlet作为服务器端代码。

请帮帮我!!!

以下是使用下拉列表的原始购物车代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Cart extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
        String s, goods[] = {"Fifa 15", "Battlefield 5", "GTA 6"};
        double price []={10,20,30};
        double cost;
        PrintWriter out = res.getWriter();
        res.setContentType("text/html");
        HttpSession session = req.getSession(true);

        if ( session == null ) return;
        for (int i = 0; i < goods.length; i++)
        if ( session.getAttribute(goods[i]) == null )
            session.setAttribute(goods[i], new Integer(0));

        if ( (s = req.getParameter("buy")) != null ) {
            int n = ((Integer)session.getAttribute(s)).intValue();
            session.setAttribute(s, new Integer(n + 1));
        }
        out.println("<html><body><h2>Shopping Cart</h2><ul>");
        for (int i = 0; i < goods.length; i++) {
            int n = ((Integer)session.getAttribute(goods[i])).intValue();
            if ( n > 0 ){
                out.println("<li><b>" + goods[i] + "</b> : " + n +":"+ price[i] +"</li>");
                cost=n*price[i];
                out.println(cost);}
            }
        out.println("</ul></body></html>");
    }
}

她是游戏的html页面:

<html>
Games
<head>
</head>
<body>
    <form  action="http://localhost:8080/apps/Cart"method="post" >

        Fifa 15 </br> 
        <img src="images/fifa-15.jpg" width = 200 height = 300 alt="image" /></br>
        <input type="submit" value="add to cart" ></br> 

        Battlefield 4</br>
        <img src="images/battlefield-4.jpg" width = 200 height = 300 alt="image" /></br>
        <input type="submit" value="add to cart" name ="battle"></br>

        GTA 5</br>
        <img src="images/gta-5.jpg" width = 200 height = 300 alt="image" /></br>
        <input type="submit" value="add to cart" name = "gta">
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

最后你的代码应该是这样的:

HttpSession session = req.getSession(true);

    for (int i = 0; i < goods.length; i++) {
        if (session.getAttribute(goods[i]) == null)
            session.setAttribute(goods[i], new Integer(0));
    }

    if ((s = req.getParameter("fifa")) != null) {
        int n = ((Integer) session.getAttribute(goods[0])).intValue();
        session.setAttribute(goods[0], new Integer(n + 1));
    } else if ((s = req.getParameter("battle")) != null) {
        int n = ((Integer) session.getAttribute(goods[1])).intValue();
        session.setAttribute(goods[1], new Integer(n + 1));
    } else if ((s = req.getParameter("gta")) != null) {
        int n = ((Integer) session.getAttribute(goods[2])).intValue();
        session.setAttribute(goods[2], new Integer(n + 1));
    }

和html页面:

    Fifa 15 </br> 
    <img src="images/fifa-15.jpg" width = 200 height = 300 alt="image" /></br>
    <input type="submit" value="add to cart" name = "fifa"></br> 

    Battlefield 4</br>
    <img src="images/battlefield-4.jpg" width = 200 height = 300 alt="image" /></br>
    <input type="submit" value="add to cart" name ="battle"></br>

    GTA 5</br>
    <img src="images/gta-5.jpg" width = 200 height = 300 alt="image" /></br>
    <input type="submit" value="add to cart" name = "gta">

在检测到按下了哪个按钮后,您可以为每个按钮选择操作。