www.pudn.com > restrant.zip > CheckoutServlet.java, change:2014-01-27,size:2584b


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.entity.OrderEntity; 
import com.alex.restrantol.entity.UserEntity; 
import com.alex.restrantol.service.CheckOutService; 
import com.alex.restrantol.util.ShoppingCartUtil; 
 
public class CheckoutServlet extends HttpServlet { 
 
	protected void service(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException { 
		String type=req.getParameter("type"); 
		UserEntity user = (UserEntity) req.getSession().getAttribute("user"); 
		if(user==null){ 
			req.getRequestDispatcher("login.jsp").forward(req, res); 
		}else{ 
			ShoppingCartUtil shoppingcart=null; 
			OrderEntity order = null; 
			if("checkout".equals(type)){ 
				shoppingcart = this.getShoppingCart(req); 
				order = this.getOrderEntity(req); 
				if(shoppingcart != null && order != null){ 
					order.setCart(shoppingcart); 
					CheckOutService out = new CheckOutService(); 
					boolean flag = out.addOrder(order); 
					if(flag){ 
						req.setAttribute("status", 1); 
					}else{ 
						req.setAttribute("status", 0); 
					} 
					req.getRequestDispatcher("/WEB-INF/status/checkoutStatus.jsp").forward(req,res); 
				} 
			}else{ 
				res.sendRedirect("/home.jsp"); 
			} 
		} 
	} 
 
	private OrderEntity getOrderEntity(HttpServletRequest req) { 
		OrderEntity order = null; 
		String name = req.getParameter("orderName"); 
		String tel = req.getParameter("orderTel"); 
		String email = req.getParameter("orderEmail"); 
		String address = req.getParameter("orderAddress"); 
		String taste = req.getParameter("radiobutton"); 
		String remark = req.getParameter("orderRemark"); 
		UserEntity user = (UserEntity) req.getSession().getAttribute("user"); 
		int customerId; 
		if(name != null && tel !=null && address != null && user != null ){ 
			customerId = user.getId(); 
			order = new OrderEntity(); 
			order.setCustomerId(customerId); 
			order.setName(name); 
			order.setTel(tel); 
			order.setEmail(email); 
			order.setAddress(address); 
			order.setTaste(taste); 
			order.setRemark(remark);	 
			order.setStatus(1); 
		} 
		return order; 
	} 
 
	protected ShoppingCartUtil getShoppingCart(HttpServletRequest req) { 
		ShoppingCartUtil shoppingcart=(ShoppingCartUtil)req.getSession().getAttribute("cart"); 
		if(shoppingcart==null){ 
			return null; 
		}else{ 
			return shoppingcart; 
		} 
	} 
 
}