romworld

JSP 29 - 쇼핑몰 만들기 사이트(11) 마지막 - [장바구니 배송,주문,취소 - ( 쿠키 활용)] 본문

WEB/JSP

JSP 29 - 쇼핑몰 만들기 사이트(11) 마지막 - [장바구니 배송,주문,취소 - ( 쿠키 활용)]

inderrom 2023. 1. 18. 17:46

배송 정보

<shippingInfo.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>배송 정보</title>
</head>
<body>
	<!-- include 액션 태그 -->
	<jsp:include page="menu.jsp" />
	<!-- ---------배송정보 시작-------------- -->
	<div class="jumbotron">
		<!-- container : 이 안에 내용 있다 -->
		<div class="container">
			<h1 class="display-3">배송정보</h1>
		</div>
	</div>	
	<div class="container">
		<!--  폼페이지 -->
		<form action="processShippingInfo.jsp" class="form-horizontal" method="post">
			<!--  폼데이터 -->
			<input type="text" name="cartId" value="<%=request.getParameter("cartId") %>" />
			<div class="form-group row">
				<lable class="col-sm-2">성명</lable>
				<div class="col-sm-3">
					<input type="text" name="name" class="form-control"  required/>
				</div>
			</div>
			<div class="form-group row">
				<lable class="col-sm-2">배송일</lable>
				<div class="col-sm-3">
					<input type="date" name="shippingDate"
						 class="form-control"  required/>(yyyy-mm-dd)
				</div>
			</div>
			<div class="form-group row">
				<lable class="col-sm-2">국가명</lable>
				<div class="col-sm-3">
					<input type="text" name="country" class="form-control" required />
				</div>
			</div>
			<div class="form-group row">
				<lable class="col-sm-2">우편번호</lable>
				<div class="col-sm-3">
					<input type="text" name="zipCode" class="form-control" required />
				</div>
			</div>
			<div class="form-group row">
				<lable class="col-sm-2">주소</lable>
				<div class="col-sm-3">
					<input type="text" name="addressName" class="form-control" required />
				</div>
			</div>
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
					<a href="cart.jsp?cartId=<%=request.getParameter("cartId")%>"
						class="btn btn-secondary" role="button">이전</a>
					<input type="submit" class="btn btn-primary" value="등록" />
					<a href="checkOutCancelled.jsp"
						 class="btn btn-secondary" role="button">취소</a>
				</div>
			</div>
		</form>
	</div>
	<!-- ---------배송정보 끝-------------- -->
	<jsp:include page="footer.jsp" />	
</body>
</html>

 

 


<processShippingInfo.jsp> 

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	request.setCharacterEncoding("UTF-8");
	// 요청 파라미터 정보들을 쿠키에 넣음
	Cookie cartId = new Cookie("shipping_cartId", request.getParameter("cartId"));
	Cookie name = new Cookie("shipping_name", request.getParameter("name"));
	Cookie shippingDate = new Cookie("shipping_shippingDate", request.getParameter("shippingDate"));
	Cookie country = new Cookie("shipping_country", request.getParameter("country"));
	Cookie zipCode = new Cookie("shipping_zipCode", request.getParameter("zipCode"));
	Cookie addressName = new Cookie("shipping_addressName", request.getParameter("addressName"));
	
	// 유효 기간을 1일로 설정(초단위)
	cartId.setMaxAge(24 * 60 * 60);
	name.setMaxAge(24 * 60 * 60);
	shippingDate.setMaxAge(24 * 60 * 60);
	country.setMaxAge(24 * 60 * 60);
	zipCode.setMaxAge(24 * 60 * 60);
	addressName.setMaxAge(24 * 60 * 60);
	
	// 생성된 쿠키를 등록
	response.addCookie(cartId);
	response.addCookie(name);
	response.addCookie(shippingDate);
	response.addCookie(country);
	response.addCookie(zipCode);
	response.addCookie(addressName);
	
	out.print("value :" + cartId.getValue() + "<br />");
	out.print("name :" + name.getValue() + "<br />");
	out.print("country :" + country.getValue() + "<br />");
	out.print("zipCode :" + zipCode.getValue() + "<br />");
	out.print("addressName :" + addressName.getValue() + "<br />");
	
	// 주문 정보 페이지로 이동
	response.sendRedirect("orderConfirmation.jsp");
%>

폼 입력 결과


 

500 에러가 나는 경우

URLEncoder.encode() 를 사용한다

예) Cookie cartId = new Cookie("shipping_cartId",
     URLEncoder.encode(request.getParameter("cartId"), "UTF-8"));

 

꺼낼 때는 URLDecoder.decode()를 사용한다

예) if(thisCookie.getName().equals("Shipping_name")){ Shipping_name = URLDecoder.decode(thisCookie.getValue(),"UTF-8"); }

 

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% //스크립틀릿
	request.setCharacterEncoding("UTF-8");
	//쿠키 쿠키 뉴 쿠키. 리스폰스 에드 쿠키
	//요청 파라미터 정보들을 쿠키에 넣음
	Cookie cartId = new Cookie("Shipping_cartId",
			URLEncoder.encode(request.getParameter("cartId"),"UTF-8"));
	Cookie name = new Cookie("Shipping_name",
			URLEncoder.encode(request.getParameter("name"),"UTF-8"));
	Cookie shippingDate 
		= new Cookie("Shipping_shippingDate",
			URLEncoder.encode(request.getParameter("shippingDate"),"UTF-8"));
	Cookie country 
		= new Cookie("Shipping_country",
			URLEncoder.encode(request.getParameter("country"),"UTF-8"));
	Cookie zipCode 
		= new Cookie("Shipping_zipCode",
			URLEncoder.encode(request.getParameter("zipCode"),"UTF-8"));
	Cookie addressName 
		= new Cookie("Shipping_addressName",
			URLEncoder.encode(request.getParameter("addressName"),"UTF-8"));
	
	//유효 기간을 1일로 설정(초단위)
	cartId.setMaxAge(24 * 60 * 60);
	name.setMaxAge(24 * 60 * 60);
	shippingDate.setMaxAge(24 * 60 * 60);
	country.setMaxAge(24 * 60 * 60);
	zipCode.setMaxAge(24 * 60 * 60);
	addressName.setMaxAge(24 * 60 * 60);
	
	//생성된 쿠키를 등록
	response.addCookie(cartId);
	response.addCookie(name);
	response.addCookie(shippingDate);
	response.addCookie(country);
	response.addCookie(zipCode);
	response.addCookie(addressName);
	
	out.print("cartId : " + cartId.getValue() + "<br />");
	out.print("name : " + name.getValue() + "<br />");
	out.print("shippingDate : " + shippingDate.getValue() + "<br />");
	out.print("country : " + country.getValue() + "<br />");
	out.print("zipCode : " + zipCode.getValue() + "<br />");
	out.print("addressName : " + addressName.getValue() + "<br />");
	//주문 정보 페이지로 이동
	response.sendRedirect("orderConfirmation.jsp");
%>

 


 

주문 정보

<orderConfirmation.jsp>

<%@page import="java.math.BigDecimal"%>
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>주문 정보</title>
</head>
<body>
	<% //스크립틀릿
		String Shipping_name = "";//개똥이
		String Shipping_zipCode = "";//12345
		String Shipping_country = "";
		String Shipping_addressName = "";
		String Shipping_shippingDate = "";
		String Shipping_cartId = "";
		
		Cookie[] cookies = request.getCookies();
		//쿠키의 개수만큼 반복
		for(int i=0;i<cookies.length;i++){
			Cookie thisCookie = cookies[i];
			
			if(thisCookie.getName().equals("Shipping_name")){
				Shipping_name = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_zipCode")){
				Shipping_zipCode = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_country")){
				Shipping_country = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_addressName")){
				Shipping_addressName = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_shippingDate")){
				Shipping_shippingDate = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_cartId")){
				Shipping_cartId = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
		}
	%>
	<!-- include 액션 태그 -->
	<jsp:include page="menu.jsp" />
	<!-- ---------주문정보 시작-------------- -->
	<div class="jumbotron">
		<!-- container : 이 안에 내용 있다 -->
		<div class="container">
			<h1 class="display-3">주문정보</h1>
		</div>
	</div>
	<div class="container col-8 alert alert-info">
		<div class="text-center">
			<h1>영수증</h1>
		</div>
		<!-- 고객 정보 시작 : cookie사용 -->
		<div class="row justify-content-between">
			<strong>배송 주소</strong>
			성명 : <%=Shipping_name%><br />
			우편번호 : <%=Shipping_zipCode%><br />
			주소 : <%=Shipping_addressName%>&nbsp;<%=Shipping_country%>
		</div>
		<div class="col-4" align="right">
			<p>
				<em>배송일 : <%=Shipping_shippingDate%></em>
			</p>
		</div>
		<!-- 고객 정보 끝 : cookie사용 -->
		<!-- 상품 정보 시작 : session 사용 -->
		<div>
			<table class="table table-hover">
				<tr>
					<th class="text-center">상품명</th>
					<th class="text-center">#</th>
					<th class="text-center">가격</th>
					<th class="text-center">소계</th>
				</tr>
				<% //스크립틀릿
					//세션의 이름인 cartlist를 통해 ProductVO 타입의 상품목록을 가져와보자
					ArrayList<ProductVO> cartList =
						(ArrayList<ProductVO>)session.getAttribute("cartlist");
					double sum = 0;
					//상품 목록을 하나씩 출력해보자
					for(int i=0;i<cartList.size();i++){
						ProductVO product = cartList.get(i);
						//얼마짜리를 몇 개 샀니? => 금액(가격 * 수량)
						double total = product.getUnitPrice() * product.getQuantity();
						sum = sum + total;
						BigDecimal totalBig = new BigDecimal(total);
						BigDecimal priceBig = new BigDecimal(product.getUnitPrice());
				%>
				<tr>
					<td class="text-center"><em><%=product.getPname()%></em></td>
					<td class="text-center"><%=product.getQuantity()%></td>
					<td class="text-center"><%=priceBig%></td>
					<td class="text-center"><%=totalBig%></td>
				</tr>
				<%
					}//end for
					BigDecimal sumBig = new BigDecimal(sum);
				%>
				<tr>
					<td></td>
					<td></td>
					<td class="text-right"><strong>총액:</strong></td>
					<td class="text-center text-danger">
						<strong><%=sumBig%></strong>
					</td>
				</tr>
			</table>
			<a href="shippingInfo.jsp?cartId=<%=Shipping_cartId%>"
				class="btn btn-secondary" role="button">이전</a>
			<a href="thankCustomer.jsp" 
				class="btn btn-secondary" role="button">주문 완료</a>
			<a href="checkOutCancelled.jsp"
				class="btn btn-secondary" role="button">취소</a>
		</div>
		<!-- 상품 정보 끝 : session 사용 -->
	</div>
	<!-- ---------주문정보 끝-------------- -->
	<jsp:include page="footer.jsp" />
</body>
</html>


주문 완료

<thankCustomer.jsp>

<%@page import="java.math.BigDecimal"%>
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>주문 완료</title>
</head>
<body>
	<% //스크립틀릿
		String Shipping_name = "";//개똥이
		String Shipping_zipCode = "";//12345
		String Shipping_country = "";
		String Shipping_addressName = "";
		String Shipping_shippingDate = "";
		String Shipping_cartId = "";
		
		Cookie[] cookies = request.getCookies();
		//쿠키의 개수만큼 반복
		for(int i=0;i<cookies.length;i++){
			Cookie thisCookie = cookies[i];
			
			if(thisCookie.getName().equals("Shipping_name")){
				Shipping_name = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_zipCode")){
				Shipping_zipCode = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_country")){
				Shipping_country = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_addressName")){
				Shipping_addressName = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_shippingDate")){
				Shipping_shippingDate = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(thisCookie.getName().equals("Shipping_cartId")){
				Shipping_cartId = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
		}
	%>
	<!-- include 액션 태그 -->
	<jsp:include page="menu.jsp" />
	<!-- ---------주문완료 시작-------------- -->
	<div class="jumbotron">
		<!-- container : 이 안에 내용 있다 -->
		<div class="container">
			<h1 class="display-3">주문완료</h1>
		</div>
	</div>
	<div class="container">
		<h2 class="alert alert-danger">주문해주셔셔 감사합니다.</h2>
		<p>주문은 <%=Shipping_shippingDate%>에 배송될 예정입니다.</p>
		<p>주문번호 : <%=Shipping_cartId%></p>
	</div>
	<div class="container">
		<p><a href="products.jsp" class="btn btn-secondary">&laquo;상품 목록</a>
	</div>
	<!-- ---------주문정보 끝-------------- -->
	<jsp:include page="footer.jsp" />
	<% //스크립틀릿
		//장바구니 세션의 정보를 제거
// 		session.invalidate();
		session.removeAttribute("cartlist");
		//쿠키의 유효기간을 0으로 설정.
		for(int i=0;i<cookies.length;i++){
			Cookie thisCookie = cookies[i];
			//쿠키 이름 가져옴
	//	 		out.print(thisCookie.getName() + "<br />");
			//쿠키 값 가져옴
	//	 		out.print(URLDecoder.decode(thisCookie.getValue(),"UTF-8")+"<br />");
			//유효기간을 0으로 설정 => 쿠키 정보 삭제
			if(thisCookie.getName().equals("Shipping_name")){
				thisCookie.setMaxAge(0);
			}
			if(thisCookie.getName().equals("Shipping_zipCode")){
				thisCookie.setMaxAge(0);
			}
			if(thisCookie.getName().equals("Shipping_country")){
				thisCookie.setMaxAge(0);
			}
			if(thisCookie.getName().equals("Shipping_addressName")){
				thisCookie.setMaxAge(0);
			}
			if(thisCookie.getName().equals("Shipping_shippingDate")){
				thisCookie.setMaxAge(0);
			}
			if(thisCookie.getName().equals("Shipping_cartId")){
				thisCookie.setMaxAge(0);
			}
			
			response.addCookie(thisCookie);
		}
	%>
</body>
</html>


주문 취소

<checkOutCancelled.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title>주문 취소</title>
</head>
<body>
   <!-- include 액션 태그 -->
   <jsp:include page="menu.jsp" />
   
   <div class="jumbotron">
      <!-- container : 이 안에 내용있다 -->
      <div class="container">
         <h1 class="display-3">주문 취소</h1>
      </div>
   </div>
   
   <!-- -----------주문 취소 내용 시작 ------------------- -->
   <%
   //기존 세션 제거 후 새로 생성
  // session.invalidate();
   session.removeAttribute("cartlist");
   %>
   <div class="container">
      <h2 class="alert alert-danger">주문이 취소되었습니다.</h2>
   </div>
   <div class="container">
      <p><a href="products.jsp" class="btn btn-secondary">&laquo;상품 목록</a></p>
   </div>
   
   <!-- -----------주문 취소 내용 끝 ------------------- -->
   
   <jsp:include page="footer.jsp" />
</body>
</html>

Comments