简易购物车Servlet类完成ITeye - 超凡娱乐

简易购物车Servlet类完成ITeye

2019-01-10 20:40:47 | 作者: 浩邈 | 标签: 购物车,产品,如果 | 浏览: 1357

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     }

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表超凡娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章