⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopcarprocessor.java

📁 网上订餐系统的源码
💻 JAVA
字号:
package com.tx.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

import com.tx.entity.Book;

public class ShopCarProcessor extends HttpServlet {

	/**
	 * 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");
		String type = request.getParameter("procType");
		if (type != null) {
			// 清空购物车
			if (type.equals("removeAll")) {
				request.getSession().removeAttribute("shopCar");
				response.sendRedirect("shopCarList.jsp");

			}
			// 删除购物车中某一个商品
			if (type.equals("removeOne")) {
				String bookId = request.getParameter("bookId");
				ShopCar car = (ShopCar) request.getSession().getAttribute(
						"shopCar");
				car.removeBook(bookId);
				response.sendRedirect("shopCarList.jsp");
			}
			// 批量更新购物车商品数量
			if (type.equals("updateCounts")) {
				// 页面上循环产生每一个书籍的编号和对应的数量,
				// 服务器端获取到对应的编号和数量数组,其中的元素也是一一对应的
				String[] bookIds = request.getParameterValues("bookIds");
				String[] counts = request.getParameterValues("count");
				ShopCar car = (ShopCar) request.getSession().getAttribute(
						"shopCar");// 得到购物车
				// 循环需要更改的商品编号数组
				for (int i = 0; i < bookIds.length; i++) {
					Book book = car.findBookById(bookIds[i]);
					book.setCount(Integer.parseInt(counts[i]));
					car.addBook(book);
				}
				request.getSession().setAttribute("shopCar", car);
				response.sendRedirect("shopCarList.jsp");
			}
		}

	}
}

⌨️ 快捷键说明

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