본문 바로가기

안녕하세요!

프로그래밍 언어/JSP

쇼핑몰 연습(1)

[ Welcome.jsp ]

<html>
<head>
<link rel = "stylesheet"
	href = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<title>Welcome</title>
</head>
<body>
	<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>
<%! 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>
<main role = "main">
<div class = "container">
	<div class = "text-center">
		<h3>
			<%=tagline %>
		</h3>
	</div>
	<hr>
</div>
</main>
<footer class="container">
	<p>&copy; WebMarket</p>
</footer>"

</body>
</html>

 

[ addProduct.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content=Type" content="text/html; charset=UTF-8">
<title>상품 등록</title>

<style>
.inputRow {
	margin: 15px 0px;
	display: flex;
	align-items: center;
}

.inputRow label {
	width: 150px;
}

.inputRow input, .inputRow textarea {
	font-szie: 1.3rem;
}

.inputRow input.btn {
	font-size: 1rem;
	padding: 5px 15px
}
</style>
</head>
<body>
	<jsp:include page="header.jsp" />
	
	<div class="main">
		<div class="banner">
			<div class="container">
				<h1>상품 등록</h1>
			</div>
		</div>
	</div>
	
	<div class="content">
		<div class="container">
			<form name="newProduct" action="./processAddProduct.jsp"
				class="form-horizontal" method="post">
				<div class="inputRow">
					<label for="productId">상품 코드</label>
					<input type="text" name="productId" id="productId">
				</div>
				<div class="inputRow">
					<label for="name">상품명</label>
					<input type="text" name="name" id="name">
				</div>
				<div class="inputRow">
					<label for="unitPrice">가격</label>
					<input type="text" name="unitPrice" id="unitPrice">
				</div>
				<div class="inputRow">
					<label for="description">상세 정보</label>
					<textarea name="description" cols="50" rows="2" id="description"></textarea>
				</div>
				<div class="inputRow">
					<label for="manufacturer">제조사</label>
					<input type="text" name="manufacturer" id="manufacturer">
				</div>
				<div class="inputRow">
					<label for="category">분류</label>
					<input type="text" name="category" id="category">
				</div>
				<div class="inputRow">
					<label for="unitStock">재고수</label>
					<input type="text" name="unitStock" id="unitStock">
				</div>
				<div class="inputRow">
					<input type="submit" value="등록" class="btn btn-secondary">
				</div>
			</form>
			<hr>
		</div>
	</div>
	
	<jsp:include page="footer.jsp" />
</body>
</html>

[ processAddProduct.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="Product" %>
<%@ page import="ProductRepository" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	request.setCharacterEncoding("UTF-8");

	String productId = request.getParameter("productId");
	String name = request.getParameter("name");
	String unitPrice = request.getParameter("unitPrice");
	String description = request.getParameter("description");
	String manufacturer = request.getParameter("manufacturer");
	String category = request.getParameter("category");
	String unitInStock = request.getParameter("unitInStock");
	String condition = request.getParameter("condition");
	
	Integer price;
	
	if(unitPrice.isEmpty())
		price = 0;
	else
		price = Integer.parseInt(unitPrice);
	
	long stock;
	
	if(unitInStock.isEmpty())
		stock = 0;
	else
		stock = Long.parseLong(unitInStock);
	
	ProductRepository dao = ProductRepository.getInstance();
	
	Product newProduct = new Product();
	newProduct.setProductId(productId);
	newProduct.setPname(name);
	newProduct.setUnitPrice(price);
	newProduct.setDescription(description);
	newProduct.setManufaturer(manufacturer);
	newProduct.setCategory(category);
	newProduct.setUnitInStock(stock);
	newProduct.setCondition(condition);
	
	dao.addProduct(newProduct);
	
	response.sendRedirect("Products.jsp");

%>


</body>
</html>

[ Product.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="Product" %>
<%@page import="ProductRepository" %>
<jsp:useBean id="productDAO" class="dao.ProductRepository" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>상품 상세 정보</title>
<style>
	.content .row {
		padding : 30px 0;
	}
	.content h3, .content p, .content h4 {
		margin : 25px 0;
	}
	.content h3 {
		margin-bottom : 5px;
	}
	.content .description {
		margin-top : 5px;
	}
	.content . badge {
		background-color : #f00;
		color : #fff;
		border-radius: 5px;
	}
</style>
</head>
<body>
	<jsp:include page="header.jsp" />
	<div class="main">
		<div class="banner">
			<div class="container">
				<h1>상품 정보</h1>
			</div>
		</div>
		
		<%
			String id = request.getParameter("id");
			ProductRepository dao = ProductRepository.getInstance();
			Product product = dao.getProductById(id);
		%>
		<div class="content">
			<div class="container">
				<div class="row">
					<h3><%=product.getPname() %></h3>
					<p class="description"><%=product.getDescription() %></p>
					<p>
						<b>상품 코드 : </b><span class="badge"><%=product.getProductId() %></span>
					<p>
						<b>제조사</b>	 : 
						<%=product.getManufaturer() %></p>
					<p>
						<b>분류</b>	 : 
						<%=product.getCategory() %></p>
					<p>
						<b>재고 수</b>	 : 
						<%=product.getUnitInStock() %>
					</p>
					<h4><%=product.getUnitPrice()%>원</h4>
					<p>
						<a href="#" class="btn btn-secondary">상품 주문 &raquo;</a> 
						<a href="./products.jsp" class="btn">상품 목록 &raquo;</a>
					</p>
				</div>\
				<hr>
			</div>
		</div>
		
	</div>
<jsp:include page="footer.jsp" />
</body>
</html>

[ ProductRepository.jsp ]

package ch02;

import java.util.ArrayList;
import Product;

public class ProductRepository {

	private ArrayList<Product> listOfProducts = new ArrayList<Product>();
	private static ProductRepository instance = new ProductRepository();
	
	public static ProductRepository getInstance() {
		return instance;
	}
	
	public ProductRepository() {
		Product phone = new Product("p1234", "iPhone 6s", 800000);
		phone.setDescription("4.7-inch, 1334X750 Renina HD display, 8-mergapixel iSight Camera");
		phone.setCategory("Smart Phone");
		phone.setManufaturer("Apple");
		phone.setUnitInStock(1000);
		phone.setCondition("New");
		
		Product notebook = new Product("P12345", "LG PC 그램", 1500000);
		notebook.setDescription("13.3-inch, IPS LED display, 5rd Generation notebook. Inter Core processor");
		notebook.setCategory("Notebook");
		notebook.setManufaturer("LG");
		notebook.setUnitInStock(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.setManufaturer("Samsung");
		tablet.setUnitInStock(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;
	}
	public void addProduct(Product product) {
		listOfProducts.add(product);
	}
	

}

[ Products.jsp ]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="Product" %>
<%@ page import="ProductRepositorty" %>
<jsp:useBean id="productDAO" class="dao.ProductRepository" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>상품 목록</title>

<style>
.main .content .row {
	display: flex;
	justify-content: space-around;
	align-items: center;
	flex-wrap : wrap;
}

.main .content .row .column {
	width: 300px;
	display: flex;
	flex-direction: column;
	justify-content: center;
	margin: 15px 0;
}

.main .content .row .column h3, .main .content .row .column p {
	text-align: center;
	padding: 10px;
}

.main .content .row .column h3 {
	font-size: 1.7rem;
	font-weight: 400;
}
</style>
<%!String greenting = "상품목록";%>
</head>
<body>
	<jsp:include page="header.jsp" />
	<div class="main">
		<div class="banner">
			<div class="container">
				<h1><%=greenting %></h1>
			</div>
		</div>
		
		<div class="content">
			<%
				ProductRepository dao = ProductRepository.getInstance();
				ArrayList<Product>listOfProducts = dao.getAllProducts();
			%>
			<div class="container">
				<div class="row">
					<%
						for(int i = 0; i < listOfProducts.size(); i++) {
							Product product = listOfProducts.get(i);
					%>
					<div class="column">
						<h3><%=product.getPname() %></h3>
						<p><%=product.getDescription() %></p>
						<p><%=product.getUnitPrice() %>원</p>
						<p>
							<a href=".product.jsp?id=<%=product.getProductId() %>"
								class="btn" role="button">상세 정보&raquo;</a>
					</div>
					<%
					}
					%>
				</div>
				<hr>
			</div>
			
		</div>
	</div>
	<jsp:include page="footer.jsp" />
</body>
</html>
728x90
반응형

'프로그래밍 언어 > JSP' 카테고리의 다른 글

JSP_22-10-28(2)  (0) 2022.10.28
JSP_22-10-28(1)  (0) 2022.10.28
JSP_22.10.27  (0) 2022.10.28
JSP_22-10-25  (0) 2022.10.26

loading