티스토리 뷰
dto/product.java
package dto;
import java.io.Serializable;
public class product implements Serializable {
private static final long serialVersionUID = -4274700572038677000L;
private String productId;
private String pname;
private Integer unitprice;
private String description;
private String manufacturer;
private String category;
private long unitsInStock;
private String condition;
private String filename;
private int quantity;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public product() {
super();
// TODO Auto-generated constructor stub
}
public product(String productId, String pname, Integer unitprice) {
super();
this.productId = productId;
this.pname = pname;
this.unitprice = unitprice;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getUnitprice() {
return unitprice;
}
public void setUnitprice(Integer unitprice) {
this.unitprice = unitprice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getUnitsInStock() {
return unitsInStock;
}
public void setUnitsInStock(long unitsInStock) {
this.unitsInStock = unitsInStock;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
추가된 부분
private int quantity;
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"%>
<%@ page import = "dto.product" %>
<%@ page import="dao.productrepository" %>
<%@ page errorPage="exceptionnoproductid.jsp" %>
<jsp:useBean id = "productDAO" class="dao.productrepository" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="EUC-KR">
<title>상품 상세 정보</title>
<script type="text/javascript">
function addtocart(){
if (confirm("상품을 장바구니에 추가하시겠습니까?")){
document.addform.submit();
}
else{
document.addform.rest();
}
}
</script>
</head>
<body>
<jsp:include page="menu.jsp"></jsp:include>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">상품 정보</h1>
</div>
</div>
<%
String id = request.getParameter("id");
productrepository dao = productrepository.getInstance();
product product = dao.getProductById(id);
//String save = request.getServletContext().getRealPath("./resources/images");
%>
<div class="container">
<div class="row">
<div class="col-md-5">
<img src="./resources/images/<%=product.getFilename() %>"
style="width:100%">
</div>
<div class="col-md-6">
<h3><%=product.getPname() %></h3>
<p><%=product.getDescription() %>
<p> <b>상품 코드 : </b><span class="badge badge-danger">
<%=product.getProductId()%></span>
<p> <b>제조사</b> : <%=product.getManufacturer() %>
<p> <b>분류</b> : <%=product.getCategory() %>
<p> <b>재고 수</b> : <%=product.getUnitsInStock() %>
<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>
</div>
</div>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
추가부분
<script type="text/javascript">
function addtocart(){
if (confirm("상품을 장바구니에 추가하시겠습니까?")){
document.addform.submit();
}
else{
document.addform.rest();
}
}
</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>
</div>
상품 주문을 클릭하면 addcart()로 이동하도록 설정했다.
장바구니 버튼을 클릭하면 cart.jsp 이동하도록 했다.
그리고 form의 action으로 addcart.jsp로 이동하도록 했는데 이동시 id를 파라미터로 가지고 가도록 했다.
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
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(id)){
cnt++;
int orderquantity = goodsqnt.getQuantity() +1;
goodsqnt.setQuantity(orderquantity);
}
}
if (cnt==0){
goods.setQuantity(1);
list.add(goods);
}
response.sendRedirect("product.jsp?id=" +id);
%>
</body>
</html>
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="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<%
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="padding-top : 50px">
<table class="table table-hover">
<tr>
<th>상품</th>
<th>가격</th>
<th>수량</th>
<th>소계</th>
<th>비고</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><%=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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
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");
%>
</body>
</html>
deletecart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="dto.product" %>
<%@ page import="dao.productrepository" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("cartid");
if(id == null || id.trim().equals("")){
response.sendRedirect("cart.jsp");
return;
}
session.invalidate();
response.sendRedirect("cart.jsp");
%>
</body>
</html>
결과
'코딩 > JSP' 카테고리의 다른 글
[쉽게 배우는 JSP 웹 프로그래밍 연습문제] 4장 (0) | 2023.12.19 |
---|---|
[쉽게 배우는 JSP 웹 프로그래밍 연습문제] 13장 (+5번 문제 추후 추가) (1) | 2023.12.18 |
[11주 1일차] 세션 (1) | 2023.12.18 |
[쉽게 배우는 JSP 웹 프로그래밍 연습문제] 3장 (1) | 2023.12.18 |
[쉽게 배우는 JSP 웹 프로그래밍 연습문제] 2장 (1) | 2023.12.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday