Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

기록

장바구니 페이지 출력 본문

JSP

장바구니 페이지 출력

9400 2023. 1. 18. 12:40
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>장바구니</title>
<link rel="stylesheet" href="/css/bootstrap.min.css"/>
</head>
<body>
<%
	//세션의 고유 아이디(장바구니 번호)
	String cartId = session.getId();
	out.print("<p>"+cartId+"</p>");

%>
	<!-- include 액션 태그 -->
	<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="shoppingInfo.jsp?cartId=<%=cartId%>" class="btn btn-success">주문하기</a>
				</td>
			</tr>
			</table>		
		</div>
		</div>
		<!-- 장바구니 출력시작 -->
		<!-- padding-top :영역의 위쪽 여백50픽셀-->
		<div style="padding-top:50px;">
			<table class="table table=hover">
				<tr>
					<th>상품</th>
					<th>가격</th>
					<th>수량</th>
					<th>금액</th>
					<th>비고</th>
				</tr>
				<%
				//ArrayList<ProductVO>
				//addCart.jsp의 cartlist라는 세션 속성명에 list를 매핑
				
				ArrayList<ProductVO> cartList = (ArrayList<ProductVO>)session.getAttribute("cartlist");
				double sum=0; //금액을 누적하는 변수
				
				
				if(cartList==null){ //장바구니가 비어있다면
				%>
				<tr style="text-align:center;">
					<td colspan="5" style="text-align:center;">장바구니에 상품이 없습니다.</td>
				</tr>
				
				<%
				}else{ 
					DecimalFormat df = new DecimalFormat("###,###");
					//상품목록
					for(ProductVO product : cartList){
						//금액 = 가격x수량
						double total = product.getUnitPrice() * product.getQuantity();
						//실수를 정수로 바꾸쟈
						BigDecimal totalBig = new BigDecimal(total);
						BigDecimal priceBig = new BigDecimal(product.getUnitPrice());
						//금액이 누적(변변대) 됨
						sum = sum + total;
				%>
				<tr>
					<td><%=product.getProductId() %>-<%=product.getPname() %></td>
					<td><%=df.format(priceBig) %>원</td>
					<td><%=product.getQuantity() %></td>
					<td><%=df.format(totalBig) %>원</td>
					<td>삭제</td>
				</tr>
				<%
					} //end for
					//총액 : 금액의 누적 합계
					BigDecimal sumBig = new BigDecimal(sum);
				%>
					<tr>
						<td></td>
						<td></td>
						<td>총액</td>
						<td><%=df.format(sumBig) %></td>
						<td></td>
					</tr>
			</table>
				<% 
				} //end if
				%>
			<a href="products.jsp" class="btn btn-secondary">&laquo;쇼핑계속하기</a>
		</div>
	
	
	<jsp:include page="footer.jsp"/> 
</body>
</html>

 

 

장바구니 삭제 
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	// 요청URI : http://localhost/deleteCart.jsp?cartId=A1D312969B5F295CEA28D635E65404BA
	// 요청파라미터 : cartId=A1D312969B5F295CEA28D635E65404BA
	//session.getId() : 세션 고유아이디 = 고유장바구니 = 웹 브라우저
	
	String id = request.getParameter("cartId");
	
	//cartId가없네? => cart.jsp로 이동
	if(id==null||id.trim().equals("")){
		response.sendRedirect("cart.jsp");
		return;
	}
	
	//장바구니 비우기
	session.removeAttribute("cartlist"); //세선 한 건만 삭제
//	session.invalidate(); //모든 세션을 삭제(로그아웃)
	
	//cart.jsp로 이동
	response.sendRedirect("cart.jsp");
	
%>

 

 

 

Comments