자바 시스템 내부/외부 서로 객체 또는 데이터를 공유할 수 있도록
바이트(byte) 형태로 데이터를 변환하고 반대로 다시
바이트 형태의 데이터를 객체 또는 데이터로 변환(역직렬화)하는 기술
package dto;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID =
-4274708574239677000L;
//멤버변수
private String productId; //상품 아이디
private String pname; //상품명
private Integer uniPrice; //상품 가격
private String description; //상품 설명
private String manufacturer; //상품 설명
private String category; //분류
private long unitsInStock; //재고 수
private String condition; //신상품 or 중고품 or 재생품
//기본 생성자
public Product() {
super();
}
//생성자(필수 : madatory 입력 처리)..(참고..선택:optional)
public Product(String productId, String pname, Integer uniPrice) {
super();
this.productId = productId;
this.pname = pname;
this.uniPrice = uniPrice;
}
//Getter()/Setter()메서드
public String getProductId() {
return productId;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getUniPrice() {
return uniPrice;
}
public void setUniPrice(Integer uniPrice) {
this.uniPrice = uniPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
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;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", pname=" + pname + ", uniPrice=" + uniPrice + ", description="
+ description + ", category=" + category + ", unitsInStock=" + unitsInStock + ", condition=" + condition
+ "]";
}
}
ProductRepository.java
package ch04.com.dao;
import java.util.ArrayList;
import java.util.List;
import dto.Product;
//자바빈즈로 사용할 상품 데이터 접근 클래스
public class ProductRepository {
private List<Product> listOfProducts = new ArrayList<Product>();
//생성자
public ProductRepository() {
Product phone = new Product("P1234", "iPhone 13s", 800000);
phone.setDescription("4.7-inch, 1334X750 Renina HD display");
phone.setCategory("Smart Phone");
phone.setManufacturer("Apple");
phone.setUnitsInStock(1000);
phone.setCondition("New");
Product notebook = new Product("P12345", "LG PC 그램", 1500000);
notebook.setDescription("13.3-inch, IPS LED display, 5th Generation");
notebook.setCategory("Notebook");
notebook.setManufacturer("LG");
notebook.setUniPrice(1000);
notebook.setCondition("Refurbished");
Product tablet = new Product("P1236", "Galaxy Tab S", 900000);
tablet.setDescription("212.8*125.6*6.6mm, Super AMOLED display");
tablet.setCategory("Tablet");
tablet.setManufacturer("Samsung");
tablet.setUnitsInStock(1000);
tablet.setCondition("Old");
//List<Product> 객체 타입의 변수에 저장
listOfProducts.add(phone);
listOfProducts.add(notebook);
listOfProducts.add(tablet);
}//end constructor
//객체 타입의 변수 listOfProducts에 저장된 모든 상품 목록을 가져옴
public List<Product> getAllProducts(){
return listOfProducts;
}
}
products.jsp
<%@page import="dto.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title>상품 목록</title>
</head>
<body>
<jsp:useBean id="productDAO" class="ch04.com.dao.ProductRepository" />
<!-- top.jsp 인클루드 시작 -->
<jsp:include page="/ch03/top.jsp" />
<!-- top.jsp 인클루드 끝 -->
<!-- jumbotron 스타일 시작 -->
<div class="jumbotron">
<div class="container">
<h1 class="display-3">상품 목록</h1>
</div>
</div>
<!-- jumbotron 스타일 끝 -->
<%
List<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>
<p><%=product.getUniPrice() %></p>
</div>
<%
} //end for
%>
<!-- 상품 반복 끝 -->
</div>
</div>
<!-- bottom.jsp 인클루드 시작 -->
<jsp:include page="/ch03/bottom.jsp"></jsp:include>
<!-- bottom.jsp 인클루드 끝 -->
</body>
</html>
products2.jsp
<%@page import="dto.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title>상품 목록</title>
</head>
<body>
<jsp:useBean id="productDAO" class="ch04.com.dao.ProductRepository" />
<!-- top.jsp 인클루드 시작 -->
<jsp:include page="/ch03/top.jsp" />
<!-- top.jsp 인클루드 끝 -->
<!-- jumbotron 스타일 시작 -->
<div class="jumbotron">
<div class="container">
<h1 class="display-3">상품 목록</h1>
</div>
</div>
<!-- jumbotron 스타일 끝 -->
<%
//자바영역
List<Product> listOfProducts = productDAO.getAllProducts();
%>
<c:set var="listOfProducts" value="<%=listOfProducts%>" />
<!-- 자바영역을 jsp세계로 소환( var="listOfProducts" ) -->
<div class="container">
<div class="row" align="center">
<!-- 상품 반복 시작 -->
<c:forEach var="product" items="${listOfProducts}">
<%-- ${listOfProducts}는 jsp영역 --%>
<div class="col-md-4">
<h3>${product.pname}</h3>
<p>${product.description}</p>
<p>${product.uniPrice}</p>
</div>
</c:forEach>
<!-- 상품 반복 끝 -->
</div>
</div>
<!-- bottom.jsp 인클루드 시작 -->
<jsp:include page="/ch03/bottom.jsp"></jsp:include>
<!-- bottom.jsp 인클루드 끝 -->
</body>
</html>