728x90
반응형
웹 쇼핑몰 만들기 01 >>> https://rogi221.tistory.com/93
웹 쇼핑몰 만들기 02 >>> https://rogi221.tistory.com/101
웹 쇼핑몰 만들기 03 >>> https://rogi221.tistory.com/105
웹 쇼핑몰 만들기 04 >>> https://rogi221.tistory.com/109
13. 장바구니 페이지 만들기
장바구니 페이지 만들기
세션을 이용하여 장바구니 페이지 만들기
- 상품 클래스에 멤버 변수 추가하기
// dto/Product.java
package dto;
import java.io.Serializable;
public class Product implements Serializable{
... (생략) ...
private String filename; // 이미지 파일명
private int quantity; // 장바구니에 담은 개수
... (생략) ...
- 추가된 멤버 변수의 Setter/Getter() 메소드 작성하기
// dto/Product.java
package dto;
import java.io.Serializable;
public class Product implements Serializable{
... (생략) ...
public int getQuantity() {return quantity;}
public void setQuantity(int quantity) {this.quantity = quantity;}
... (생략) ...
- 상품 상세 정보 페이지 수정하기
// product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
... (생략) ...
<title>상품 상세 정보</title>
<script type="text/javascript">
function addToCart() {
if(confirm("상품을 장바구니에 추가하시겠습니까?")) {
document.addForm.submit();
} else {
document.addForm.reset();
}
}
</script>
... (생략) ...
<h4><%= product.getUnitPrice() %>원</h4>
<p> <form name="addForm" action="./addCart.jsp?id=<%=product.getProductId()%>" method="post">
<a href="#" class="btn btn-info" onclick="addToCart()">상품 주문 »</a>
<a href="./cart.jsp" class="btn btn-warning">장바구니 »</a>
<a href="./products.jsp" class="btn btn-secondary">상품 목록 »</a>
</form>
... (생략) ...
- 장바구니에 등록하는 페이지 작성하기:
// addCart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dto.Product" %>
<%@ page import="dao.ProductRepository" %>
<%
String id = request.getParameter("id");
if (id == null || id.trim().equals("")) {
response.sendRedirect("products.jsp");
return;
}
ProductRepository dao = ProductRepository.getInstance();
Product product = dao.getProductById(id);
if(product == null) {
response.sendRedirect("exceptionNoProductId.jsp");
}
ArrayList<Product> goodsList = dao.getAllProducts();
Product goods = new Product();
for(int i=0; i < goodsList.size(); i++) {
goods = goodsList.get(i);
if(goods.getProductId().equals(id)) {
break;
}
}
ArrayList<Product> list = (ArrayList<Product>) session.getAttribute("cartlist");
if(list == null) {
list = new ArrayList<Product>();
session.setAttribute("cartlist", list);
}
int cnt = 0;
Product goodsQnt = new Product();
for(int i=0; i < list.size(); i++) {
goodsQnt = list.get(i);
if(goodsQnt.getProductId().equals(is)) {
cnt++;
int orderQuantity = goodsQnt.getQuantity() + 1;
goodsQnt.setQuantity(orderQuantity);
}
}
if(cnt == 0) {
goods.setQuantity(1);
list.add(goods);
}
response.sendRedirect("product.jsp?id=" + id);
%>
- 장바구니 페이지 작성하기
// cart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dto.Product" %>
<%@ page import="dao.ProductRepository" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./resources/css/bootstrap.min.css"/>
<%
String cartId = session.getId();
%>
<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">
<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="#" class="btn btn-success">주문하기</a></td>
</tr>
</table>
</div>
<div style="paddint-top: 50px">
<table class="table table-hover">
<tr>
<th>상품</th>
<th>가격</th>
<th>수량</th>
<th>소개</th>
<th>비고</th>
</tr>
<%
int sum = 0;
ArrayListM<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><%=product.getProductId()%> - <%=product.getPname()%></td>
<td><%=product.getUnitPrice()%></td>
<td><%=product.getQuantity()%></td>
<td><%=total%></td>
<td><a href="./removeCart.jsp?id=<%=product.getProductId()%>" class="badge badge-danger">삭제</a></td>
</tr>
<%
}
%>
<tr>
<th></th>
<th></th>
<th>총액</th>
<th><%=sum%></th>
<th></th>
</tr>
</table>
<a href="./products.jsp" class="btn btn-secondary">« 쇼핑 계속하기</a>
</div>
<hr>
</div>
<jsp:include page="footer.jsp"/>
</body>
</html>
- 장바구니에 등록된 개별 상품 삭제 페이지 작성하기:
// removeCart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dto.Product" %>
<%@ page import="dao.ProductRepository" %>
<%
String id = request.getParameter("id");
if (id == null || id.trim().equals("")) {
response.sendRedirect("products.jsp");
return;
}
ProductRepository dao = ProductRepository.getInstance();
Product product = dao.getProductById(id);
if(product == null) {
response.sendRedirect("exceptionNoProductId.jsp");
}
ArrayList<Product>) cartList = (ArrayList<Product>) session.getAttribute("cartlist");
Product goodsQnt = new Product();
for (int i=0; i<cartList.size(); i++) {
goodsQnt = cartList.get(i);
if(goodsQnt.getProductId().equals(id)) {
cartList.remove(goodsQnt);
}
}
response.sendRedirect("cart.jsp");
%>
- 장바구니에 등록된 전체 상품 삭제 페이지 작성하기:
// deleteCart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="dto.Product"%>
<%@ page import="dao.ProductRepository" %>
<%
String id = request.getParameter("cartId");
if (id == null || id.trim().equals("")) {
response.sendRedirect("cart.jsp");
return;
}
session.invalidate();
response.sendRedirect("cart.jsp");
%>
- 나중에 추가 -
웹 쇼핑몰 만들기 01 >>> https://rogi221.tistory.com/93
웹 쇼핑몰 만들기 02 >>> https://rogi221.tistory.com/101
웹 쇼핑몰 만들기 03 >>> https://rogi221.tistory.com/105
웹 쇼핑몰 만들기 04 >>> https://rogi221.tistory.com/109
728x90
반응형
'JSP' 카테고리의 다른 글
JSP 웹 프로그래밍 - 웹 쇼핑몰 만들기 06 (주문 처리 페이지, 상품 관리 테이블 생성 및 상품 등록하기) (0) | 2023.03.07 |
---|---|
JSP 웹 프로그래밍 - 세션 (세션의 개요, 생성, 정보, 삭제, 유효 시간 설정) (0) | 2023.03.07 |
JSP 웹 프로그래밍 - 필터 02 (web.xml 파일의 필터 구성) (0) | 2023.03.07 |
JSP 웹 프로그래밍 - 필터 01(필터의 개요, Filter 인터페이스의 구현 클래스) (0) | 2023.03.07 |
JSP 웹 프로그래밍 - 예외처리 (0) | 2023.03.06 |