shoppingservlet.java

来自「购物车程序」· Java 代码 · 共 58 行

JAVA
58
字号
/**
 * application name        ShoppingServlet.java
 * copyright               Copyright  2008 东软 实训中心版权所有
 * company                 neusoft
 * time                    2008-8-1
 *
 * @author             	 王洪雁
 * @version              ver 1.0
 */
package javaweb.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javaweb.dto.*;

public class ShoppingServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "text/html; charset=GBK";

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		ServletContext context = getServletContext();
		HttpSession session = request.getSession();
		ShopCart cart = (ShopCart) session.getAttribute("shopCart");
		String action = request.getParameter("action");
		if ("remove".equals(action)) {
			String removeId = request.getParameter("removeId");
			cart.removeProductFromCart(removeId);
		} else if ("purchase".equals(action)) {
			String[] productIds = request.getParameterValues("productId");

			Map products = (Map) context.getAttribute("products");
			if (cart == null) {
				cart = new ShopCart();
				session.setAttribute("shopCart", cart);
			}
			if (productIds == null) {
				productIds = new String[0];
			}
			for (int i = 0; i < productIds.length; i++) {
				Product product = (Product) products.get(productIds[i]);
				cart.addProductToCart(product);
			}
		}
		RequestDispatcher rd = request.getRequestDispatcher("/servlet/showcart");
		rd.forward(request, response);
	}

	// Process the HTTP Post request
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?