티스토리 뷰

코딩/JSP

웹쇼핑몰

ehzim 2023. 12. 8. 15:54

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;
	
	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;
	}
	
	
	
}

 

 

dao/productrepository.java

package dao;

import java.util.ArrayList;

import dto.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 idsplay, 8-megapixel iSight Camera");
		phone.setCategory("Smart Phone");
		phone.setManufacturer("Apple");
		phone.setUnitsInStock(1000);
		phone.setCondition("new");
		phone.setFilename("P1234.png");
		
		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.setUnitsInStock(1000);
		notebook.setCondition("Refurbished");
		notebook.setFilename("P1235.png");
		
		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.setUnitsInStock(1000);
		tablet.setCondition("Old");
		tablet.setFilename("P1236.png");
		
		listOfProducts.add(phone);
		listOfProducts.add(notebook);
		listOfProducts.add(tablet);
	}
	public ArrayList<product> getAllProducts(){
		return listOfProducts;
	}
	
	public product getProductById(String productId) {
		product productBtId=null;
		
		for(int i=0; i<listOfProducts.size(); i++) {
			product product = listOfProducts.get(i);
			if(product != null && product.getProductId() != null && product.getProductId().equals(productId)) {
				productBtId = product;
				break;
			}
		}
		return productBtId;
	}
	public void addproduct(product product) {
		listOfProducts.add(product);
	}
}

 

product.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" %>
<!-- 
<jsp:useBean id="productDAO" class="dao.productrepository" scope="session"/> 객체 생성을 두번이나 하잖아 멍청아 지워야지-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<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">
</head>
<body>
	<jsp:include page="menu.jsp" />
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">상품 목록</h1>
		</div>
	</div>
	<%--
		ArrayList<product> listOfProducts = productDAO.getAllProducts();
		arraylist 생성하는 방법 바꿔잖아 밑에껄로 근데 왜 삭제를 안해 멍청아
	--%>
	<%
		productrepository dao = productrepository.getInstance();
		ArrayList<product> listOfproducts = dao.getAllProducts();
	%>
	<div class="container">
		<div class="row" align="center">
			<%
				for(int i=0; i<listOfproducts.size(); i++){
					product product = listOfproducts.get(i);
					//String save = request.getServletContext().getRealPath("./resources/images");
			%>
	
			<div class="col-md-4">
				<img src="./resources/images/<%=product.getFilename() %>" 
				style="width:100%">
				<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;</a>
			</div>
			
			<%
				}
			%>
		</div>
		<hr>
	</div>

	<jsp:include page="footer.jsp"/>
</body>
</html>

 

product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "dto.product" %>
<%@ page import="dao.productrepository" %>
<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>
</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> <a href="#" class="btn btn-info"> 상품 주문 &raquo;</a>
					<a href="./products.jsp" class="btn btn-secondary"> 상품 목록 &raquo;</a>
			</div>
		</div>
	</div>
	
	<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>

 

addproduct.jsp

0<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
	<jsp:include page="menu.jsp"></jsp:include>
	<div class="jumbotron">

		<div class="container">
			<form name="newproduct" action="./processaddproduct.jsp"
			class="form-horizontal" method="post" enctype="multipart/form-data">
				<h1 class="display-3">상품 등록</h1>
				<div class="form-group row">
					<label class="col-sm-2">상품 코드</label>
						<div class="col-sm-3">
							<input type="text" name="productid" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상품명</label>
						<div class="col-sm-3">
							<input type="text" name="name" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">가격</label>
						<div class="col-sm-3">
							<input type="text" name="unitprice" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상세 정보</label>
						<div class="col-sm-5">
							<textarea name="description" rows="2" cols="50" class="form-control"></textarea>
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">제조사</label>
						<div class="col-sm-3">
							<input type="text" name="manufacturer" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">분류</label>
						<div class="col-sm-3">
							<input type="text" name="category" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">재고 수</label>
						<div class="col-sm-3">
							<input type="text" name="untisinstock" class="form-control">
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상태</label>
						<div class="col-sm-5">
							<input type="radio" name="condition" class="new ">
							신규 제품
							<input type="radio" name="condition" class="old">
							중고 제품
							<input type="radio" name="condition" value="refurbished">
							재생 제품
						</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">이미지</label>
					<div class="col-sm-5">
						<input type="file" name="productimage" class="form-control">
					</div>
				</div>	
				<div class="form-group row">
					<div class="col-sm-offset-2 col-sm-10">
						<input type="submit" class="btn btn-primary" value="등록">
					</div>
			</div>
		</form>
	</div>
</body>
</html>

 

 

processaddproduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>
<%@ page import="java.util.*" %>
<%@ page import="dto.product" %>
<%@ page import="dao.productrepository" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");

	String filename="";
	String save = request.getServletContext().getRealPath("./resources/images");
	String realfolder = save;
	int maxsize = 5*1024*1024;
	String enctype = "utf-8";
	
	MultipartRequest multi = new MultipartRequest(request, realfolder,maxsize, enctype, new DefaultFileRenamePolicy());
	

	String productid =multi.getParameter("productid");
	String name = multi.getParameter("name");
	String unitprice = multi.getParameter("unitprice");
	String description = multi.getParameter("description");
	String manufacturer = multi.getParameter("manufacturer");
	String category = multi.getParameter("category");
	String unitsinstock = multi.getParameter("untisinstock");
	String condition = multi.getParameter("condition");
	
	Integer price;
	
	if(unitprice.isEmpty())
		price=0;
	
	else
		price = Integer.valueOf(unitprice);
	
	long stock;
	
	if(unitsinstock.isEmpty())
		stock=0;
	else
		stock=Long.valueOf(unitsinstock);
	
	/*Enumeration files = multi.getFileNames();
	String fname = (String) files.nextElement();
	String filename1 = multi.getFilesystemName(fname);*/
	
	
	String fname=(String) multi.getParameter("productimage");
	//String filename= multi.getFilesystemName(fname);

	

	
	productrepository dao =productrepository.getInstance();
	
	product newproduct = new product();
	newproduct.setProductId(productid);
	newproduct.setPname(name);
	newproduct.setUnitprice(price);
	newproduct.setDescription(description);
	newproduct.setManufacturer(manufacturer);
	newproduct.setCategory(category);
	newproduct.setUnitsInStock(stock);
	newproduct.setCondition(condition);
	newproduct.setFilename(fname);
	
	dao.addproduct(newproduct);
	
	response.sendRedirect("products.jsp");
%>
</body>
</html>

 

 

 

 

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday