주문하기 - 쿠키 이용

조수경·2022년 1월 20일
0

JSP

목록 보기
38/45

쿠키생성 방법

  • Cookie cookie = new Cookie(String name, String value);

쿠키 정보 받기

  • Request.getCookie() 메소드를 사용
  • 쿠키 객체를 얻어온 후 getName() 메소드를 통해 쿠키 이름을 가져옴
  • getValue() 메소드를 통해 해당 이름의 쿠키의 값을 얻음

쿠키 삭제

  • 쿠키를 더 유지할 필요가 없다면 유효 기간을 만료하기
  • setMaxAge(0)

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>
<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->

	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">배송 정보</h1>
		</div>
	</div>
	<div class="container">
		<form method="post" class="form-horizontal"
		action="shippingInfo_process.jsp">
			<input type="hidden" name="cartId" value="${param.cartId}" />
			<div class="form-group row">
				<label class="col-sm-2">성명</label>
				<div class="col-sm-3">
					<input type="text" name="name" class="form-control" />
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">배송일</label>
				<div class="col-sm-3">
					<input type="text" name="shippingDate" class="form-control" />
				(yyyy/mm/dd)
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">국가</label>
				<div class="col-sm-3">
					<input type="text" name="country" class="form-control" />
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">우편번호</label>
				<div class="col-sm-3">
					<input type="text" name="zipCode" class="form-control" />
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">주소</label>
				<div class="col-sm-3">
					<input type="text" name="addressName" class="form-control" />
				</div>
			</div>
			
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
				     <a href="cart.jsp?cartId=${param.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>
<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</body>
</html>

shippingInfo_process.jsp

<%@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 객체명 = new Cookie(name,value);
    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"));
	//생성한 쿠키의 유효 기간을 24시간으로 설정(1초)
	cartId.setMaxAge(24*60*60); //0으로 하면 쿠키가 사라짐 / 60이면 1분
	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객체를 통해 클라이언트(크롬)에개 전달해 줌
	response.addCookie(cartId);
	response.addCookie(name);
	response.addCookie(shippingDate);
	response.addCookie(country);
	response.addCookie(zipCode);
	response.addCookie(addressName);
	
	response.sendRedirect("orderConfirmation.jsp");
	
%>

orderConfirmation.jsp

<%@page import="java.net.URLDecoder"%>
<%@page import="dto.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	request.setCharacterEncoding("UTF-8");

    String cartId = session.getId();

    Cookie[] cookies = request.getCookies();//쿠키를 싹 가져와서
    //이름값들을 전역변수로 빼줌
    String shipping_cartId ="";
    String shipping_name ="";
    String shipping_shippingDate ="";
    String shipping_country ="";
    String shipping_zipCode ="";
    String shipping_addressName ="";
    //쿠키가 있어야 함
    if(cookies != null){
    	for(int i=0;i<cookies.length;i++){//갯수만큼 반복문을 돌릴건데~
    		Cookie thisCookie = cookies[i];//쿠키의 몇번째 갯수를 가져와서~
    		//쿠키명을 가져오기(명, 값)
    		String n = thisCookie.getName();//이름을 가져오기
    		//쿠키명에 매핑되어 있는 값을 가져오기
    		if(n.equals("Shipping_cartId")){
    			shipping_cartId = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
    		}
    		if(n.equals("Shipping_name")){
    			shipping_name = URLDecoder.decode(thisCookie.getValue(),"UTF-8");//각 변수에 담기
    		}
    		if(n.equals("Shipping_shippingDate")){
    			shipping_shippingDate = URLDecoder.decode(thisCookie.getValue(),"UTF-8");//각 변수에 담기
    		}
    		if(n.equals("Shipping_country")){
    			shipping_country = URLDecoder.decode(thisCookie.getValue(),"UTF-8");//각 변수에 담기
    		}
    		if(n.equals("Shipping_zipCode")){
    			shipping_zipCode = URLDecoder.decode(thisCookie.getValue(),"UTF-8");//각 변수에 담기
    		}
    		if(n.equals("Shipping_addressName")){
    			shipping_addressName = URLDecoder.decode(thisCookie.getValue(),"UTF-8");//각 변수에 담기
    		}
    
    		
    	}
    }
    
%>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>주문 정보</title>
</head>
<body>
<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->

	<div class="jumbotron">
		<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>
		<!-- 배송에 관란 정보 시작 -->
	<div class="row justify-content-between">
		<div class="col-4" align="left">
		<strong>배송 주소</strong><br />
		성명 : <%=shipping_name %><br />
		우편번호 : <%=shipping_zipCode %><br />
		주소 : <%=shipping_addressName%>(<%=shipping_country%>)<br />
	  </div>
	  <div class="col-4" align="right">
	  	<p><em>배송일 : <%=shipping_shippingDate%></em></p>
	  	</div>
	</div>
	<!-- 배송에 관란 정보 끝 -->
	<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)
				List<Product> cartList = (List<Product>)session.getAttribute("cartlist");
			%>
			<c:set var="cartList" value="<%=cartList%>" />
			<!-- 상품에 관한 정보 시작 -->
			<c:forEach var="product" items="${cartList}">
			<tr>
				<th class="text-center"><em>${product.pname}</em></th>
				<th class="text-center">${product.quantity}</th>
				<th class="text-center"><fmt:formatNumber value="${product.uniPrice}" pattern="#,###" /></th>
				<th class="text-center"><fmt:formatNumber value="${product.quantity*product.uniPrice}" pattern="#,###" /></th>
			</tr>
			<!-- sum으로 누적 -->
				<c:set var="sum" value="${sum + product.uniPrice*product.quantity}" />
			</c:forEach>
			<!-- 상품에 관한 정보 끝 -->
			<tr>
				<td></td>
				<td></td>
				<td class="text-right"><strong>총액 : </strong></td>
				<td class="text-center text-danger"><strong><fmt:formatNumber value="${sum}" pattern="#,###" /></strong></td>
			
			</tr>
		</table>
		<!-- ?cartId=7B55AA8801A881CA51D1CAAFC42DFFB0 -->
		 <a href="cart.jsp?cartId=${param.cartId}" class="btn btn-secondary" role="button">이전</a>
		 <a href="thankCustomer.jsp" class="btn btn-success" role="button">주문 완료</a>
		 <a href="checkOutCancelled.jsp" class="btn btn-secondary" role="button">취소</a>
		</div>
	</div>
<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</body>
</html>

thankCustomer.jsp

<%@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" %>
<%
	Cookie[] cookies = request.getCookies();

	String shipping_cartId = "";
	String shipping_shippingDate = "";

	if(cookies != null){
		for(int i=0;i<cookies.length;i++){
			Cookie thisCookie = cookies[i];
			String n = thisCookie.getName();
			if(n.equals("Shipping_cartId")){
				shipping_cartId = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
			if(n.equals("Shipping_shippingDate")){
				shipping_shippingDate = URLDecoder.decode(thisCookie.getValue(),"UTF-8");
			}
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>주문 완료</title>
</head>
<body>

<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->

	<div class="jumbotron">
		<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>
		</p>
	</div>
<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->

</body>
</html>

<%
	//세션으로 저장된 장바구니 정보를 모두 삭제함
	session.invalidate();
	//쿠키에 저장된 배송 정보를 모두 삭제함
	for(int i=0;i<cookies.length;i++){
		Cookie thisCookie = cookies[i];//쿠키의 몇번째 갯수를 가져와서~
		//쿠키명을 가져오기(명, 값)
		String n = thisCookie.getName();//이름을 가져오기
		//쿠키명에 매핑되어 있는 값을 가져오기
		if(n.equals("Shipping_cartId")){
			thisCookie.setMaxAge(0);
		}
		if(n.equals("Shipping_name")){
			thisCookie.setMaxAge(0);
		}
		if(n.equals("Shipping_shippingDate")){
			thisCookie.setMaxAge(0);
		}
		if(n.equals("Shipping_country")){
			thisCookie.setMaxAge(0);
		}
		if(n.equals("Shipping_zipCode")){
			thisCookie.setMaxAge(0);
		}
		if(n.equals("Shipping_addressName")){
			thisCookie.setMaxAge(0);
		}
		
		response.addCookie(thisCookie);
		
	}
%>

checkOutCancelled.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>
<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->

	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">주문 취소</h1>
		</div>
	</div>
	<div class="container">
		<h2 class="alert alert-danger">주문이 취소되었습니다.</h2>
	</div>
	<div class="container">
		<p>
			<a href="porducts.jsp" class="btn btn-secondary">
				&laquo; 상품 목록
			</a>
		</p>
	</div>

<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</body>
</html>

profile
신입 개발자 입니다!!!

0개의 댓글