【问题标题】:Shopping Cart Using Servlet使用 Servlet 的购物车
【发布时间】:2014-12-19 07:44:18
【问题描述】:

我有一个简单的购物车代码,可以从 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>

【问题讨论】:

    标签: java javascript session servlets shopping-cart


    【解决方案1】:

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

    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">
    

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

    【讨论】:

    • 还是不行,你用 system.out.println('fifa') 替换的代码最初是用来将下拉列表中的项目添加到购物车的。但下拉列表不再存在
    • 我添加了 id=fifa 而不是 name=fifa 但出现错误。
    • 我又试了一次,我认为是在正确的轨道上。有趣的是,当按下按钮时,购物车页面仍然是空的,但我注意到 fifa 显示在我运行服务器的 cmd 上。你怎么看?
    • 你明白了!如果您希望您的产品出现在购物车中,您需要以适当的方式将其存储在会话中。
    • 好的,fifa 已显示在购物车页面中,您能帮我计算一下显示价格的计算方法吗? if ( (s = req.getParameter("fifa")) != null ) { out.println("fifa"); //int n = ((Integer)session.getAttribute(s)).intValue(); //session.setAttribute(s, new Integer(n + 1));
    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 2021-03-17
    • 2017-11-05
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多