1. 程式人生 > >購物車操作

購物車操作

加入購物車

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

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


public class Add2CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//設定編碼
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		//獲取商品的名稱
		String name=request.getParameter("name");
		name=new String(name.getBytes("iso-8859-1"),"utf-8");
		//將商品新增到購物車中
		//從session中獲取購物車
		Map<String,Integer> car=(Map<String, Integer>) request.getSession().getAttribute("cart");
		Integer count=null;
		//判斷購物車是否為空
		if(car==null){
			//第一次購物   建立購物車
			car=new HashMap<String, Integer>();
			//將購物車放入session中
			request.getSession().setAttribute("cart", car);
			count=1;
		}else{
			//購物車不為空,繼續判斷購物車中是否有該商品
			count=car.get(name);
			if(count==null){
				//購物車沒有該商品
				count=1;
			}else{
				//購物車中有該商品
				count++;
			}
		}
		//將商品放入到購物車中
		car.put(name, count);
		writer.println("已經將<b>"+name+"</b>新增到購物車中<hr/>");
		writer.print("<a href='/cookie/product_list.jsp'>繼續購物</a>");
		writer.print("<a href='/cookie/cart.jsp'>檢視購物車</a>");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

cart.jsp

<%@ page import="java.util.Map" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center" width="50%">
		<tr>
			<td>商品名稱</td>
			<td>數量</td>
		
		</tr>
		<%
		//1.獲取購物車
		Map<String,Integer> map = (Map<String,Integer>)session.getAttribute("cart");
		//2.判斷購物車是否為空
		if(map==null){
			//2.1若為空
			out.print("<tr><td colspan='2'>親,購物車空空,先逛逛</td></tr>");
		}else{
			//2.2若不為空  遍歷購物車
			for(String name:map.keySet()){
				out.print("<tr>");
				
				out.print("<td>");
				out.print(name);
				out.print("</td>");
				
				out.print("<td>");
				out.print(map.get(name));
				out.print("</td>");
				
				out.print("</tr>");
				
				
			}
			
		}
		
		%>
		
	
	
	</table>
	<hr/>
	<center>
		<a href="/cookie/product_list.jsp">繼續購物</a>
		<a href="/cookie/clearcart">清空購物車</a>
	
	</center>
	

</body>
</html>

清空購物車

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ClearCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//清空session
		request.getSession().invalidate();
		//重定向
		response.sendRedirect(request.getContextPath()+"/cart.jsp");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}