Study/WebServer

[웹 쇼핑몰 실습] 주문 처리 페이지 만들기

Gyuri 2021. 8. 22. 23:54

쿠키를 이용해 주문 처리 페이지 만들기

  1. 장바구니 페이지 수정 (cart.jsp 파일 추가 작성)
    <a href="./shippingInfo.jsp?cartId=<%=cartId %>" class="btn btn-success">주문하기</a>​
  2. 배송 정보 페이지 작성하기 (shippingInfo.jsp 파일 생성 후 다음과 같이 작성)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
    <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">
    		<form action="./processShippingInfo.jsp" class="form-horizontal" method="post">
    			<input type="hidden" name="cartId" value="<%=request.getParameter("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=<%=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>
    </body>
    </html>​
  3. 배송 정보 처리 페이지 작성 (processShippingInfo.jsp 파일 생성 후 다음과 같이 작성)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@page import="java.net.URLEncoder"%>
    
    	<%
    		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("cartId"), "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"));
    		
    		cartId.setMaxAge(24 * 60 * 60); // 유효기간 24시간으로 설정(24 x 60x 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);
    	%>​
  4. 주문 정보 페이지 작성 (orderConfirmation.jsp 파일 생성 후 다음과 같이 작성)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@page import="java.net.URLDecoder"%>
    <%@page import="dao.ProductRepository" %>
    <%@page import="dto.Product" %>
    <%@page import="java.util.ArrayList" %>
    
    	<%
    		request.setCharacterEncoding("UTF-8");
    
    		String cartId = session.getId(); // 고유한 세션 내장 객체의 아이디 가져옴
    		
    		String shipping_cartId = "";
    		String shipping_name = "";
    		String shipping_shippingDate = "";
    		String shipping_country = "";
    		String shipping_zipCode = "";
    		String shipping_addressName = "";
    		
    		Cookie[] cookies = request.getCookies();
    		
    		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");
    			}
    		}
    	%>
    	
    <html>
    <link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
    <head>
    <meta charset="UTF-8">
    <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 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>
    				성명 : <%out.println(shipping_name); %> <br>
    				우편번호 : <%out.println(shipping_zipCode); %> <br>
    				주소 : <%out.println(shipping_addressName); %> <br>
    				(<%out.println(shipping_country); %>) <br>
    			</div>
    			<div class="col-4" align="right">
    				<p><em>배송일 : <%out.println(shipping_shippingDate); %></em>
    			</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>
    			<%
    				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 class="text-center"><em><%=product.getPname()%></em></td>
    				<td class="text-center"><em><%=product.getQuantity()%></em></td>
    				<td class="text-center"><em><%=product.getUnitPrice()%>원</em></td>
    				<td class="text-center"><em><%=total%>원</em></td>
    			</tr>
    			<%
    				}
    			%>
    			<tr>
    				<td></td>
    				<td></td>
    				<td class="text-right"><string>총액:</string></td>
    				<td class="text-center" text-danger"><string><%=sum %></string></td>
    			</tr>
    			</table>
    			
    			<a href="./shippingInfo.jsp"?cartId=<%=shipping_cartId %>"class="btn btn-secondary" role="button">이전</a>
    			<a href="./thankCustomer.jsp"? class="btn btn-success" role="button">주문 완료</a>
    			<a href="./checkOutCancelled.jsp.jsp"? class="btn btn-secondary" role="button">취소</a>
    			
    		</div>
    	</div>
    </body>
    </html>​
  5. 주문 완료 페이지 작성 (thankCustomer.jsp 파일 생성 후 다음과 같이 작성)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="java.net.URLDecoder"%>
    
    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
    <meta charset="UTF-8">
    <title>주문 완료</title>
    </head>
    <body>
    	<%
    		request.setCharacterEncoding("UTF-8");
    
    		String cartId = session.getId(); // 고유한 세션 내장 객체의 아이디 가져옴
    		
    		String shipping_cartId = "";
    		String shipping_name = "";
    		String shipping_shippingDate = "";
    		String shipping_country = "";
    		String shipping_zipCode = "";
    		String shipping_addressName = "";
    		
    		Cookie[] cookies = request.getCookies();
    		
    		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");
    			}
    		}
    	%>
    	<jsp:include page="menu.jsp"/>
    	<div class="jumbotron">
    		<div class="container">
    			<h1 class="display-3">주문 완료</h1>
    		</div>
    	</div>
    	<div class="conatiner">
    		<p><a href="./products.jsp" class="btn btn-secondary"> &laquo; 상품 목록</a>
    	</div>
    </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);
    	}
    %>​
  6. 주문 취소 페이지 작성 (checkOutCancelled.jsp 파일 생성 후 다음과 같이 작성)
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
    <head>
    <meta charset="UTF-8">
    <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">
    		<h2 class="alert alert-danger">주문이 취소되었습니다.</h2>
    	</div>
    	<div class="container">
    		<p><a href="./products.jsp" class="btn btn-secondary"> &laquo; 상품 목록</a>
    	</div>
    </body>
    </html>​

 

실행결과 (배송 정보)

 

 

실행결과 (주문 정보)

 

 

실행결과 (주문 취소)