주문하기 요청하면 배송정보페이지
cart.jsp 주문하기 버튼 수정
<a href="./shipping.jsp?cartId=<%=cartId%>" class="btn btn-success">주문하기</a>
shipping.jsp 파일 생성
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>배송정보</title>
<link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
</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="./processShipping.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 name="name" type="text" class="form-control" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">배송일</label>
<div class="col-sm-3">
<input name="shippingDate" type="date" class="form-control" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">국가명</label>
<div class="col-sm-3">
<input name="country" type="text" class="form-control" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">우편번호</label>
<div class="col-sm-3">
<input name="zipCode" type="text" class="form-control" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">주소</label>
<div class="col-sm-5">
<input name="addressName" type="text" 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="./orderCancel.jsp" class="btn btn-secondary"
role="button">취소</a>
</div>
</div>
</form>
</div>
</body>
</html>
이미 장바구니 정보는 세션에 저장되어있음.
화면 출력

취소 버튼 눌렀을 경우 취소페이지로 이동
orderCancel.jsp 파일 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<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">
<h2 class="alert alert-danger">주문이 취소되었습니다.</h2>
</div>
<div class="container">
<p><a href="./books.jsp" class="btn btn-secondary">« 도서목록</a>
</div>
</body>
</html>

shipping.jsp에서 등록 버튼 누르면
processShipping.jsp으로 이동함
processShipping.jsp파일 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder"%>
<%
/* 쿠키는 한글을 저장하면 깨지는 현상이 생겨 한글을 미리 인코딩해서 저장하고 꺼낼 때 디코딩한다. */
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"));
/* 쿠키 생존 시간 (초 단위 )*/
cartId.setMaxAge(24 * 60 * 60);//24시간
name.setMaxAge(24 * 60 * 60);
zipCode.setMaxAge(24 * 60 * 60);
country.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);
response.sendRedirect("orderConfirmation.jsp");
%>
** 쿠키에 주문 배송 정보를 저장한다.
***setMaxAge : 쿠키 생존 시간 (초 단위)
**** <%@ page import="java.net.URLEncoder"%> : 쿠키를 한글로 저장 시 URLEncoder 클래스를 사용해서 저장 시 encode를 한다.
Cookie cartId = new Cookie("Shipping_cartId", URLEncoder.encode(request.getParameter("cartId"), "utf-8"));
저장할 이름, 실제 데이터
-response.sendRedirect("orderConfirmation.jsp"); orderConfirmation.jsp로 보내기
출력 화면
orderConfirmation.jsp는 아직 생성되지 않았으므로 , 쿠키 생성 확인하면 됨.


쿠키 생존 시간 확인 가능함
현재 시각 9월 26일 이므로
24시간 지난 시간으로 확인 가능함
orderConfirmation.jsp 생성
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.net.URLDecoder"%>
<%@ page import="dto.Book"%>
<%@ page import="dao.BookRepository"%>
<%
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();
/* 쿠키가 null이 아니면 쿠키를 다시 디코딩해서 하나씩 가져온다. */
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>
<head>
<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 col-8 alert alert-info">
<div class="text-center ">
<h1>영수증</h1>
</div>
<div class="row justify-content-between">
<div class="clo-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>
</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<Book> cartList = (ArrayList<Book>) session.getAttribute("cartlist");
if (cartList == null)
cartList = new ArrayList<Book>();
for (int i = 0; i < cartList.size(); i++) { // 도서 리스트 하니씩 출력하기
Book book = cartList.get(i);
int total = book.getUnitPrice() * book.getQuantity();
sum = sum + total;
%>
<tr>
<td class="text-center"><em><%=book.getName()%></em></td>
<td class="text-center"><%=book.getQuantity()%></td>
<td class="text-center"><%=book.getUnitPrice()%>원</td>
<td class="text-center"><%=total%>원</td>
</tr>
<%
}
%>
<tr>
<td></td>
<td></td>
<td class="text-right"><strong>총액: </strong></td>
<td class="text-center text-danger"><strong><%=sum%></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-success"
role="button">주문완료</a>
<a href="./orderCancel.jsp" class="btn btn-secondary"
role="button">취소</a>
</div>
</div>
</body>
</html>
<%@ page import="java.net.URLDecoder"%> 디코더로 인코딩 한 내용 다시 가져올 수 있음.
출력 화면

주문완료 버튼 누르면
주문완료 페이지 출력
thankCustomer.jsp 파일 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder"%>
<html>
<head>
<link rel="stylesheet" href="./resources/css/bootstrap.min.css" />
<title>주문 완료</title>
</head>
<body>
<%
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_shippingDate"))
shipping_shippingDate = 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="container">
<h2 class="alert alert-danger">주문해주셔서 감사합니다.</h2>
<p>주문은 <%out.println(shipping_shippingDate);%>에 배송될 예정입니다!
<p>주문번호 : <%out.println(shipping_cartId);%>
</div>
<div class="container">
<p><a href="./books.jsp" class="btn btn-secondary">« 도서목록</a>
</div>
</body>
</html>
<%
/* 모든 세션 정보 삭제하기 */
session.invalidate();
/* 쿠키 생존 시간을 0초로 바꾸어서 삭제함 */
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);
}
%>
화면 출력



쿠키 정보 다 삭제
'BACKEND > Jsp' 카테고리의 다른 글
북마켓 프로젝트 10 (0) | 2023.09.27 |
---|---|
북마켓 프로젝트 9 (0) | 2023.09.26 |
북마켓 프로젝트 7 (0) | 2023.09.25 |
북마켓 프로젝트 6 (0) | 2023.09.25 |
북마켓 프로젝트 5 (0) | 2023.09.22 |