JAVA/JSP

[JSP] 구현

아잠만_ 2024. 6. 28. 15:00

wecome.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
	<!-- header -->
	<%@ include file="menu.jsp" %>
	
	<%! // 선언문
		// 전역변수
		String greeting = "Welcome to My Page";
		String tagline = "I want to sleep";
	%>
	<!-- 표현문 -->
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3"><%=greeting %></h1>
		</div>
	</div>
	<div class="container">
		<div class="text-center">
			<h3><%=tagline %></h3>
			<%
				Date day = new Date();
				int hour = day.getHours();
				String min = "0"+day.getMinutes();
				String sec = "0"+day.getSeconds();
				String am_pm;
				if(hour/12==0){
					am_pm ="AM";
				} else {
					am_pm = "PM";
					hour = hour-12;
				}
				String hourStr = "0"+hour;
				hourStr = hourStr.substring(hourStr.length()-2,hourStr.length());
				min = min.substring(min.length()-2,min.length());
				sec = sec.substring(sec.length()-2,sec.length());
				String CT = hourStr + ":" + min + ":" + sec + " " + am_pm;
				
				
				out.print("<p>"+CT+"</p>");
			%>
		</div>
	</div>
	<!-- footer -->
	<%@ include file="footer.jsp" %>
</body>
</html>

menu.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
	<nav class="navbar navbar-expand navbar-dark bg-dark">
		<div class="container">
			<div class="navbar-header">
				<a class="navbar-brand" href="/welcome.jsp">Home</a>
			</div>
		</div>
	</nav>

footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<footer class="container">
		<p>&copy; 오리</p>
	</footer>

기본 세팅

cmd에서 oracle user추가

C:\Users\PC-13>sqlplus sys/java@localhost:1521 as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 금 6월 28 14:54:24 2024

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


다음에 접속됨:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> create user jsptest identified by java;

사용자가 생성되었습니다.

SQL> grant connect, resource to jsptest;

권한이 부여되었습니다.

table 생성

create table product(
    product_id varchar2(20),
    pname varchar2(150),
    unit_price number,
    description varchar2(1000),
    manufacturer varchar2(90),
    category varchar2(60),
    units_in_stock number,
    condition varchar2(60),
    constraint pk_product primary key(product_id)
);

설명 추가

java - ProductVO생성

ProductVO.java
package kr.or.ddit.vo;

// 자바 빈 클래스
/*  자바빈 규약
  	1. 프로퍼티
  	2. 기본생성자
  	3. getter/setter메서드
 */
public class ProductVO {
	// 프로퍼티 = 멤버변수(필드)
	private String productId;
	private String pname;
	private long unitPrice;
	private String description;
	private String manufacturer;
	private String category;
	private long unitsInStock;
	private String condition;
	
	// 기본 생성자
	public ProductVO() {
	}

	// getter / setter 메서드
	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 long getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(long 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;
	}

	@Override
	public String toString() {
		return "ProductVO [productId=" + productId + ", pname=" + pname + ", unitPrice=" + unitPrice + ", description="
				+ description + ", manufacturer=" + manufacturer + ", category=" + category + ", unitsInStock="
				+ unitsInStock + ", condition=" + condition + "]";
	}
}

dao클래스

ProductRepository.java
package kr.or.ddit.dao;

import java.util.ArrayList;
import java.util.List;

import kr.or.ddit.vo.ProductVO;

public class ProductRepository {
	// 싱글톤 객체
	private static ProductRepository dao;
	
	public static ProductRepository getInstance() {
		if(dao==null) dao = new ProductRepository();
		return dao;
	}
	
	private List<ProductVO> listOfProducts = new ArrayList<ProductVO>();
	
	// 기본 생성자 3개의 상품 정보를 설정
	// db에서 가져온다고 가정
	private ProductRepository() {
		ProductVO phone = new ProductVO("P1234", "iPhone 15s", 1250000);
		phone.setDescription("iPhone 15 · A16 Bionic 칩 5코어 GPU 탑재 · 첨단 듀얼 카메라 시스템. 48MP 메인 카메라 2배 망원 지원 울트라 와이드 카메라 · 최대 26시간 동영상 재생 ");
		phone.setCategory("Smart Phone");
		phone.setManufacturer("Apple");
		phone.setUnitsInStock(1000);
		phone.setCondition("New");
		
		ProductVO notebook = new ProductVO("P1235", "LG Gram", 2000000);
		notebook.setDescription("최대 22시간의 배터리 성능이 선사하는 궁극의 프로급 휴대성");
		notebook.setCategory("NoteBook");
		notebook.setManufacturer("LG");
		notebook.setUnitsInStock(1000);
		notebook.setCondition("Old");
		
		ProductVO tablet = new ProductVO("P1236", "갤럭시 탭 S9 Ultra", 1700000);
		tablet.setDescription("디스플레이 369.9 mm 다이나믹 아몰레드 2X · 내구성 IP68 · 프로세서 스냅드래곤8 2세대");
		tablet.setCategory("Tablet");
		tablet.setManufacturer("Samsung");
		tablet.setUnitsInStock(1000);
		tablet.setCondition("Refurbished");
		
		listOfProducts.add(phone);
		listOfProducts.add(notebook);
		listOfProducts.add(tablet);
	}
	
	// ProductVO 객체 타입의 변수 listOfProducts에 저장된 모든 상품 목록을 가져옴
	public List<ProductVO> getAllProducts(){
		return listOfProducts;
	}
}

JSP에서 상품목록 가져오기

product.jsp
<%@page import="kr.or.ddit.vo.ProductVO"%>
<%@page import="java.util.List"%>
<%@page import="kr.or.ddit.dao.ProductRepository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>상품 목록</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
	<!-- header -->
	<%@ include file="menu.jsp" %>
	<% // 스크립틀릿
	ProductRepository dao = ProductRepository.getInstance();
	List<ProductVO> list = dao.getAllProducts();
	%>
	<!-- 상품 목록 시작 -->
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">Product List</h1>
		</div>
	</div>
	<div class="container">
		<div class="row" align="center">
		<c:set var="list" value="<%=list %>"/>
		<!-- 상품 반복 부분 시작 -->
		<c:forEach var="vo" items="${list}">
			<div class="col-md-4">
				<h3>${vo.pname}</h3>
				<p>${vo.description}</p>
				<p><fmt:formatNumber value="${vo.unitPrice}" type="number" pattern="#,###" />원</p>
			</div>
		</c:forEach>		
		<!-- 상품 반복 부분 끝 -->
		</div>
	</div>
	<!-- 상품 목록 끝 -->
	
	<!-- footer -->
	<%@ include file="footer.jsp" %>	
</body>
</html>