jsp電子商務購物車之 數據庫存儲篇 序二

業務邏輯圖,簡單版要寫各個Servlethtml


//ChangeCartCountServlet 使用ajax實現數量,增長或減小;

package com.cart.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cart.dao.BookDao;
import com.cart.dao.impl.BookDaoImpl;
import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ChangeCartCountServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		int bookid = Integer.parseInt(req.getParameter("bookid"));
		int count = Integer.parseInt(req.getParameter("count"));
		CartService cartService=new CartService();
		Userinfo userinfo=(Userinfo)req.getSession().getAttribute("userinfo");
		int uid=userinfo.getId();//得到當前用戶id編號;
		
		CartItem cartItem=cartService.findCartItemById(uid,bookid);
		if(cartItem.getCount()<count){//若是根據總數量小於你增長的總數量,則不能購物,業務正確;
			out.print("false");
			return;
		}
		
		Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
		int result=-1;
		if(cart!=null){
			cartItem = cart.get(bookid); //得到購物車項		
			cartItem.setCount(count);
			cart.put(bookid, cartItem);
			
			result=cartService.updateCartItemCount(cartItem);
		}
		if(result>0){
			out.print("true");
		}else{
			out.print("false");
		}
		
		out.flush();
		out.close();
	}
}

package com.cart.web;


import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class ShowBookServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		CartService cartService=new CartService();
		Map<Integer,CartItem> cart = cartService.getCartItem(uid);
		
		session.setAttribute("cart", cart); //加入session用jstl讀取;
		resp.sendRedirect("show_cart.jsp");
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

package com.cart.web;


import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cart.entity.Book;
import com.cart.entity.CartItem;
import com.cart.entity.Userinfo;
import com.cart.service.CartService;

public class DeleteCartServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session=req.getSession();
		Userinfo user=(Userinfo)session.getAttribute("userinfo");
		int uid=user.getId();
		int bookid = Integer.parseInt(req.getParameter("bookid"));
				
				Map<Integer,CartItem> cart = (Map<Integer,CartItem>)req.getSession().getAttribute("cart");
				// 根據key(bookid)刪除
				cart.remove(bookid);
				//須要根據carService進行操做;
				CartService cartService=new CartService();
				cartService.deletecartItem(uid, bookid);
				
				req.getSession().setAttribute("cart", cart);
				
				resp.sendRedirect("show_cart.jsp");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
相關文章
相關標籤/搜索