slide-image
728x90

 

ProductRepository.java

package DAO;

import java.util.ArrayList;
import DTO.Product; 

public class ProductRepository {
	private ArrayList<Product> listOfProducts = new ArrayList<Product>();
	
	public ProductRepository() {
		Product phone = new Product("P1234", "iPhone 6s", 800000);
		phone.setDescription("4.17-inch, 1334X750 Renina HD display, 8-megapixel iSight Camera");
		phone.setCategory("Smart Phone");
		phone.setManufacturer("Apple");
		phone.setUnitsInSock(1000);
		phone.setCondition("New");
		
		Product notebook = new Product("P1235", "LG PC 그램", 1500000);
		notebook.setDescription("13.3-inch, IPS LED display, 5rd Generation Intel Core processors");
		notebook.setCategory("NoteBook");
		notebook.setManufacturer("LG");
		notebook.setUnitsInSock(1000);
		notebook.setCondition("Refurbished");
		
		Product tablet = new Product("P1236", "Galaxy Tab S", 900000);
		tablet.setDescription("212.8*125.6*6.6mm, Super AMOLED display, Octa-Core Processor");
		tablet.setCategory("Tablet");
		tablet.setManufacturer("Samsung");
		tablet.setUnitsInSock(1000);
		tablet.setCondition("Old");
		
		listOfProducts.add(phone);
		listOfProducts.add(notebook);
		listOfProducts.add(tablet);
		
	}
	
	public ArrayList<Product> getAllProducts(){
		return listOfProducts;
	}
	
	public Product getProductById(String productId) {
		Product productById = null;
		
		for(int i=0; i<listOfProducts.size(); i++) {
			Product product = listOfProducts.get(i);
			
			if(product!=null && product.getProductId()!= null && product.getProductId().equals(productId)) {
				productById = product;
				break;
			}
		}
		
		return productById;
	}
}
- 상품 목록에서 상품 아이디와 일치하는 상품을 가져오는 getProductById() 메소드 

 

 

products.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="DTO.Product" %>

<jsp:useBean id="productDAO" class="DAO.ProductRepository" scope="session" />

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 목록</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
	<%@ include file="Menu.jsp" %>
	
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">
				상품 목록
			</h1>
		</div>	
	</div>
	
	<%
		ArrayList<Product> listOfProducts = productDAO.getAllProducts();
	%>
	
	<div class="container">
		<div class="row" align="center">
			<%
				for(int i=0; i<listOfProducts.size(); i++){
					Product product = listOfProducts.get(i);
			%>
			
			<div class="col-md-4">
				<h3><%=product.getPname() %></h3>
				<p><%=product.getDescription() %>
				<p><%=product.getUnitPrice() %>원
				<p><a href="./product.jsp?id=<%=product.getProductId() %>" 
					class="btn btn-secondary" role="button">
						<!-- &raquo; = 특수문자 >> -->
						상세 정보 &raquo;
					</a>
			</div>
			
			<%
				}
			%>
		</div>
		
		<!-- 구분선 -->
		<hr>
	</div>
	
	<%@ include file="Footer.jsp" %>
</body>
</html>

 

- 상세 정보 버튼 생성

 

 

Product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="DTO.Product" %>

<jsp:useBean id="productDAO" class="DAO.ProductRepository" scope="session" />

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 상세 정보</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
		<%@ include file="Menu.jsp" %>
		
		<div class="jumbotron">
			<div class="container">
				<h1 class="display-3">
					상품 정보
				</h1>
			</div>
		</div>
		
		<%
			String id = request.getParameter("id");
			Product product = productDAO.getProductById(id);	
		%>
		
		<div class="container">
			<div class="row">
				<div class="col-md-6">
					<h3><%=product.getPname() %></h3>
					<p><%=product.getDescription() %>
					<p><b>상품 코드: </b>
					<span class="bagde 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><a href="#" class="btn btn-info">상품 주문  &raquo;</a>
					<a href="./Products.jsp" class="btn btn-secondary">상품 목록  &raquo;</a>
				</div>
			</div>
			<hr>
		</div>
		
		<%@ include file="Footer.jsp" %>
</body>
</html>
- DTO.Product 패키지를 사용하기 위해 import 사용
- ProductRepository 클래스를 사용
- 상품 목록 페이지로부터 전달되는 상품 아이디를 전송받도록 request 내장 객체의 getParameter() 메소드 작성
- getProductById() 메소드를 호출하여 반한된 결과 값을 product에 저장
- product에 저장된 상품명, 상품 상세 정보, 상품 코드, 제조사, 분류, 재고 수 , 상품 가격 등을 출력

 

 

Welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
	<%@ include file="Menu.jsp" %>
	
	<%! 
		String greeting ="Welcome to Web Shopping Mall";
		String tagline = "Welcome to Web Market!";
	%>
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">
				<%=greeting %>
			</h1>
		</div>
	</div>

	<div class="container">
		<div class="text-center">
			<h3>
				<%=tagline %>
			</h3>
			
			<!-- 현재 접속 시각 출력 -->
			<%
				//5초에 한번 새로고침
				response.setIntHeader("Refresh", 5);
				Date day = new java.util.Date();
				String am_pm;
				int hour = day.getHours();
				int minute = day.getMinutes();
				int second = day.getSeconds();
				
				if(hour/12 == 0){
					am_pm = "AM";
				}else {
					am_pm = "PM";
					hour -= 12;
				}
				
				String CT = hour + ":" + minute + ":" + second + " " + am_pm;
				out.println("현재 접속 시각: " + CT + "\n");
				
			%>
		</div>
	</div>
	
	<%@ include file="Footer.jsp" %>
	
</body>
</html>
- 5초마다 JSP 페이지가 갱신되도록 response 내장 객체의 setIntHeader() 메소드 사용

 


Welcome.jsp

Products.jsp

 

Product.jsp

 

 

WebMarket (4).zip
0.06MB

728x90
반응형

'Code > Market' 카테고리의 다른 글

07. 상품 이미지 등록하기  (0) 2020.09.29
06. 상품 등록 페이지 만들기  (0) 2020.09.29
04. 상품 목록 표시하기  (0) 2020.09.25
03. 한글 출력 및 페이지 모듈화  (0) 2020.09.25
02. 시작 페이지 만들기  (0) 2020.09.25