欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简易购物车Servlet类实现 博客分类: java 简易购物车Servlet 

程序员文章站 2024-03-18 16:31:04
...
一 CartServlet实现
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ItemsDAO;
import entity.Cart;
import entity.Items;
public class CartServlet extends HttpServlet {
    private String action ; //表示购物车的动作 ,add,show,delete
    //商品业务逻辑类的对象
    private ItemsDAO idao = new ItemsDAO();
    
    
    /**
     * Constructor of the object.
     */
    public CartServlet() {
        super();
    }
    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request,response);
    }
    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(request.getParameter("action")!=null)
        {
            this.action = request.getParameter("action");
            if(action.equals("add")) //如果是添加商品进购物车
            {
                if(addToCart(request,response))
                {
                    request.getRequestDispatcher("/success.jsp").forward(request, response);
                }
                else
                {
                    request.getRequestDispatcher("/failure.jsp").forward(request, response);
                }
            }
            if(action.equals("show"))//如果是显示购物车
            {
                request.getRequestDispatcher("/cart.jsp").forward(request, response);
            }
            if(action.equals("delete")) //如果是执行删除购物车中的商品
            {
                if(deleteFromCart(request,response))
                {
                    request.getRequestDispatcher("/cart.jsp").forward(request, response);
                }
                else
                {
                    request.getRequestDispatcher("/cart.jsp").forward(request, response);
                }
            }
        }
        
    }
    //添加商品进购物车的方法
    private boolean addToCart(HttpServletRequest request, HttpServletResponse response)
    {
        String id = request.getParameter("id");
        String number = request.getParameter("num");
        Items item = idao.getItemsById(Integer.parseInt(id));
        
        //是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象
        if(request.getSession().getAttribute("cart")==null)
        {
            Cart cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        if(cart.addGoodsInCart(item, Integer.parseInt(number)))
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }
    
    //从购物车中删除商品
    private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response)
    {
        String id = request.getParameter("id");
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        Items item = idao.getItemsById(Integer.parseInt(id));
        if(cart.removeGoodsFromCart(item))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

 

}