www.pudn.com > restrant.zip > ShoppingCartServlet.java, change:2014-01-25,size:1361b


package com.alex.restrantol.servlet; 
 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.alex.restrantol.dao.MealDao; 
import com.alex.restrantol.entity.MealEntity; 
import com.alex.restrantol.util.ShoppingCartUtil; 
 
public class ShoppingCartServlet extends HttpServlet { 
	protected void service(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException { 
		String type=req.getParameter("type"); 
		if("add".equals(type)){ 
			if(req.getParameter("mid")!=null) 
			{ 
				int mealId = new Integer(req.getParameter("mid")).intValue(); 
		 
				MealEntity meal = new MealDao().findMealById(mealId); 
				 
				this.makeShoppingCart(req).addMeal(meal); 
			} 
		}else if("clearcart".equals(type)){ 
			this.makeShoppingCart(req).clearShopCart(); 
		} 
		res.sendRedirect("shoppingCart.jsp"); 
	} 
 
	protected ShoppingCartUtil makeShoppingCart(HttpServletRequest req) { 
		ShoppingCartUtil shoppingcart=(ShoppingCartUtil)req.getSession().getAttribute("cart"); 
		if(shoppingcart==null){ 
			shoppingcart=new ShoppingCartUtil(); 
			req.getSession().setAttribute("cart", shoppingcart); 
			return shoppingcart; 
		}else{ 
			return shoppingcart; 
		} 
	} 
}