Study/WebServer

[웹 쇼핑몰 실습] 장바구니 페이지 만들기

Gyuri 2021. 8. 22. 23:46

세션 이용해 장바구니 페이지 만들기

 

상품 상세 정보 페이지 수정 

products.jsp 파일에 추가 작성

<script type="text/javascript">
	function addToCart() {
		if(confirm("상품을 장바구니에 추가하시겠습니까?")) {
			document.addForm.submit();
		}
		else {
			document.addForm.reset();
		}
	}
</script>
<form name="addForm" action="./addCart.jsp?id=<%=product.getProductId() %>" method="post">
				<p><a href="#" class="btn btn-info" onclick="addToCart()"> 상품 주문 &raquo;</a> 
				<a href="./cart.jsp" class="btn btn-warning">장바구니 &raquo;</a>
</form>

 

장바구니에 등록하는 페이지 작성 

addCart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="dto.Product"%>
<%@ page import="dao.ProductRepository"%>

<%
	String id = request.getParameter("id");
	if(id == null || id.trim().equals("")) {
		response.sendRedirect("products.jsp");
		return;
	}
	
	ProductRepository dao = ProductRepository.getInstance();
	
	Product product = dao.getProductById(id);
	if(product == null) {
		response.sendRedirect("exeptionNoProductId.jsp");
	}
	
	ArrayList<Product> goodsList = dao.getAllProducts();
	Product goods = new Product();
	for(int i=0; i<goodsList.size(); i++) {
		goods = goodsList.get(i);
		if(goods.getProductId().equals(id)) {
			break;
		}
	}
	
	ArrayList<Product> list = (ArrayList<Product>) 
			session.getAttribute("cartlist");
	if(list==null) {
		list = new ArrayList<Product>();
		session.setAttribute("cartlist", list);
	}
	
	int cnt = 0;
	Product goodsQnt = new Product();
	
	
	for(int i=0; i<list.size(); i++) {
		goodsQnt = list.get(i);
		if(goodsQnt.getProductId().equals(id)) {
			cnt++;
			int orderQuantity = goodsQnt.getQuantity() + 1;
			goodsQnt.setQuantity(orderQuantity);
		}
	}
	
	if(cnt == 0) {
		goods.setQuantity(1);
		list.add(goods);
	}
	
	response.sendRedirect("product.jsp?id=" + id);
%>

 

장바구니 페이지 작성

cart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"  %>
<%@page import="ch12.Product"%>
<%@page import="ch12.ProductRepository"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
<%
	String cartId = session.getId();
%>
<title>장바구니</title>
</head>
<body>
	<jsp:include page="menu.jsp"/>
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">장바구니</h1>
		</div>
	</div>
	
	<div class="container">
		<div class="row">
			<table width="100%">
				<tr>
					<td align="left"><a href="./deleteCart.jsp?cartId=<%=cartId %>" class="btn btn-danger">삭제하기</a></td>
					<td align="right"><a href="#" class="btn btn-success">주문하기</a></td>
				</tr>
			</table>
		</div>
	<div style="padding-top : 50px">
		<table class="table table-hover">
			<tr>
				<th>상품</th>
				<th>가격</th>
				<th>수량</th>
				<th>소계</th>
				<th>비고</th>
			</tr>
			<%
				int sum=0;
				ArrayList<Product> cartList = (ArrayList<Product>)
						session.getAttribute("cartlist");
						
				if(cartList == null) 
					cartList = new ArrayList<Product>();
				
				for(int i=0; i<cartList.size(); i++) { // 상품 리스트 하나씩 출력
					Product product = cartList.get(i);
					int total = product.getUnitPrice() * product.getQuantity();
					sum=sum+total;
			%>
			<tr>
				<td><%=product.getProductId() %> - <%=product.getPname() %> %></td>
				<td><%=product.getUnitPrice() %></td>
				<td><%=product.getQuantity() %></td>
				<td><%=total %></td>
				<td><a href="./removeCart.jsp?id=<%=product.getProductId() %>" class="badge badge-danger">삭제</a></td>
			</tr>
			<%
				}
			%>
			<tr>
				<th></th>
				<th></th>
				<th>총액</th>
				<th><%=sum %></th>
				<th></th>
			</tr>
		</table>
		<a href="./products.jsp" class="btn btn-secondary"> &laquo; 쇼핑 계속하기</a>
		</div>
		<hr>
		</div>
		<jsp:include page="footer.jsp"/>
</body>
</html>

 

 

장바구니에 등록된 개별 상품 삭제 페이지 작성

removeCart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"  %>
<%@page import="ch12.Product"%>
<%@page import="ch12.ProductRepository"%>

// 장바구니에 등록된 개별 상품 삭제 페이지

<%
	String id = request.getParameter("id");
	if(id == null || id.trim().equals("")) {
		response.sendRedirect("products.jsp");
		return;
	}
	
	ProductRepository dao = ProductRepository.getInstance();
	
	Product product = dao.getProductById(id);
	if(product == null) {
		response.sendRedirect("exceptionNoProductId.jsp");
	}
	
	ArrayList<Product> cartList = (ArrayList<Product>)session.getAttribute("cartlist");
	Product goodsQnt = new Product();
	for(int i=0; i<cartList.size(); i++) {
		goodsQnt = cartList.get(i);
		if(goodsQnt.getProductId().equals(id)) {
			cartList.remove(goodsQnt);
		}
	}
	
	response.sendRedirect("cart.jsp");
%>

 

장바구니에 등록된 전체 상품 삭제 페이지 작성

deleteCart.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="ch12.Product"%>
<%@page import="ch12.ProductRepository"%>

<%
	String id = request.getParameter("cartId");
	if(id==null || id.trim().equals("")) {
		response.sendRedirect("cart.jsp");
		return;
	}
	
	session.invalidate();
	

	response.sendRedirect("cart.jsp");
%>

 

실행결과 (아무 상품도 안 넣은 상태)