23.04.06(Jsp)

MH S·2023년 4월 6일
0

Jsp

목록 보기
14/16

쇼핑몰

ProductMgr.java

package shop;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class ProductMgr {
	private DBConnectionMgr pool;
	public static final String SAVEFOLDER = "C:/Jsp/myapp/src/main/webapp/shop/data/";
	public static final String ENCODING = "UTF-8";
	public static final int MAXSIZE = 1024 * 1024 * 20; // 20MB
	
	public ProductMgr() {
		pool = DBConnectionMgr.getInstance();
	}
	
	// Product List
	public Vector<ProductBean> getProductList(){
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		Vector<ProductBean> vlist = new Vector<ProductBean>();
		try {
			con = pool.getConnection();
			sql = "select no, name, price, date, stock from tblProduct "
					+ "order by no desc";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				ProductBean bean = new ProductBean();
				bean.setNo(rs.getInt(1));
				bean.setName(rs.getString(2));
				bean.setPrice(rs.getInt(3));
				bean.setDate(rs.getString(4));
				bean.setStock(rs.getInt(5));
				vlist.addElement(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.freeConnection(con, pstmt, rs);
		}
		return vlist;
	}
	
	// Product Detail
	public ProductBean getProduct(int no/*상품번호*/) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		ProductBean bean = new ProductBean();
		try {
			con = pool.getConnection();
			sql = "select no, name, price, detail, date, stock, image "
					+ "from tblProduct where no=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, no);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				bean.setNo(rs.getInt(1));
				bean.setName(rs.getString(2));
				bean.setPrice(rs.getInt(3));
				bean.setDetail(rs.getString(4));
				bean.setDate(rs.getString(5));
				bean.setStock(rs.getInt(6));
				bean.setImage(rs.getString(7));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.freeConnection(con, pstmt, rs);
		}
		return bean;
	}

//Product Stock Reduce (구매 -> 재고 수정)
	public void reduceProduct(OrderBean order) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = pool.getConnection();
			sql = "update tblProduct set stock = stock-? where no=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, order.getQuantity());//주문수량
			pstmt.setInt(2, order.getProductNo());//상품번호
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.freeConnection(con, pstmt);
		}
	}
	
	
	
	// admin mode
	
	// Product Insert : 상품 저장
	
	// Product Update
	
	// Product Delete
	
	
}

productList.jsp

<!-- productList.jsp -->
<%@page import="ch16.UtilMgr"%>
<%@page import="shop.ProductBean"%>
<%@page import="java.util.Vector"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="pMgr" class="shop.ProductMgr"/>
<%
	Vector<ProductBean> pvlist = pMgr.getProductList();
%>
<html>
<head>
<title>Simple Shopping Mall</title>
<script src="script.js"></script>
</head>
<body bgcolor="#996600" topmargin="100">
	<%@ include file="top.jsp" %>
	<table width="75%" align="center" bgcolor="#FFFF99">
	<tr> 
	<td align="center" bgcolor="#FFFFCC">
		<table width="95%" bgcolor="#FFFF99" border="1">
			<tr align="center" bgcolor="#996600"> 
				<td><font color="#FFFFFF">이름</font></td>
				<td><font color="#FFFFFF">가격</font></td>
				<td><font color="#FFFFFF">날짜</font></td>
				<td><font color="#FFFFFF">재고</font></td>
				<td><font color="#FFFFFF">상세보기</font></td>
			</tr>
			<%
				for(int i = 0; i<pvlist.size();i++){
					ProductBean pbean = pvlist.get(i);
			%>
			<tr align="center">
				<td><%=pbean.getName()%></td>
				<td><%=UtilMgr.monFormat(pbean.getPrice())%></td>
				<td><%=pbean.getDate()%></td>
				<td><%=UtilMgr.monFormat(pbean.getStock())%></td>
				<td><input type="button" value="상세보기"
				onclick="productDetail('<%=pbean.getNo()%>')"></td>
			</tr>		
			<%} // for %>
		</table>
	</td>
	</tr>
	</table>
	<form name="detail" method="post" action="productDetail.jsp" >
		<input type="hidden" name="no">
	</form>
	<%@ include file="bottom.jsp" %>
</body>
</html>

prodcutDetail.jsp

<%@page import="shop.ProductBean"%>
<%@page import="shop.UtilMgr"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="pMgr" class="shop.ProductMgr"/>
<%
	int no = UtilMgr.parseInt(request, "no");
	ProductBean pbean = pMgr.getProduct(no);	
%>
<html>
<head>
<title>Simple Shopping Mall</title>
<script src="script.js"></script>
</head>
<body bgcolor="#996600" topmargin="100">
<%@ include file="top.jsp" %>
<form name="cart" action="cartProc.jsp">
<table width="75%" align="center" bgcolor="#FFFF99">
	<tr> 
	<td align="center" bgcolor="#FFFFCC">
		<table width="95%" bgcolor="#FFFF99" border="1">
		<tr bgcolor="#996600"> 
		<td colspan="3" align="center">
			<font color="#FFFFFF"><%=pbean.getName() %></font>
		</td>
		</tr>
		<tr> 
		<td width="20%">
		<img src="data/<%=pbean.getImage() %>" height="150" width="150">
		</td>
		<td width="30%" valign="top">
			<table>
			<tr>
				<td><b>상품이름 : <%=pbean.getName() %></b></td>
			</tr>			
			<tr>
				<td><b>가    격 : <%=UtilMgr.intFormat(pbean.getPrice())%></b></td>
			</tr>
			<tr>
				<td><b>수    량 : </b><input name="quantity" size="5" value="1"></td>
			</tr>
			<tr>
			<td align="center">
				<input type="submit" value="장바구니 담기">
			</td>
			</tr>
			</table>
			<input type="hidden" name="productNo" value="<%=pbean.getNo()%>">	
			<input type="hidden" name="flag" value="insert">			
		</td>
		<td width="50%" valign="top">
		<b>상세설명</b><br/>
		<pre><%=pbean.getDetail()%></pre>
		</td>
		</tr>
		</table>
	</td>
	</tr>
</table>
</form>
<%@ include file="bottom.jsp" %>
</body>
</html>

CartMgr.java

package shop;

import java.util.Hashtable;

// 장바구니 기능 : DB 연동 x, 세션을 이용
public class CartMgr {
	
	// key : 상픔번호, value : 주문객체
	private Hashtable<Integer, OrderBean> hCart = 
			new Hashtable<Integer, OrderBean>();
	
	// Cart Add : 기존에 장바구니에 동일한 상품이 있는 경우 고려
	public void addCart(OrderBean order) {
		int productNo = order.getProductNo(); // 상품번호
		int quantity = order.getQuantity(); // 주문수량
		if(quantity>0) {
			if(hCart.containsKey(productNo)) {
				// 기존에 주문 객체를 가져옴
				OrderBean temp = hCart.get(productNo);
				quantity+=temp.getQuantity();
				order.setQuantity(quantity);
				hCart.put(order.getProductNo(), order);		
			} else {
				// 기존에 동일한 상품번호가 없는 경우
				hCart.put(order.getProductNo(), order);
			}
		}
	}
	
	// Cart Delete
	public void deleteCart(OrderBean order) {
		hCart.remove(order.getProductNo());
	}
	
	// Cart Update
	public void updateCart(OrderBean order){
		// 같은 key 값이 들어가면 덮어쓰기
		hCart.put(order.getProductNo(), order);
	}
	
	// Cart Get
	public Hashtable<Integer, OrderBean> getCartList(){
		return hCart;
	}
}

cartProc.jsp

<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="cMgr" scope="session" class="shop.CartMgr"/>
<jsp:useBean id="order" class="shop.OrderBean"/>
<jsp:setProperty property="*" name="order"/>
<%
	String id = (String)session.getAttribute("idKey");
	if(id==null){
		response.sendRedirect("login.jsp");
		return; // 이후에 코드를 무력화
	}
	order.setId(id);
	// insert, update, delete
	String flag = request.getParameter("flag");
	String msg = "";
	if(flag.equals("insert")){
		msg = "장바구니에 저장하였습니다.";
		cMgr.addCart(order);
	} else if(flag.equals("update")){
		msg = "장바구니를 수정 하였습니다.";
		cMgr.updateCart(order);
	} else if(flag.equals("delete")){
		msg = "장바구니를 삭제 하였습니다.";
		cMgr.deleteCart(order);
	}
%>
<script>
	alert("<%=msg%>");
	location.href = "cartList.jsp";
</script>

cartList.jsp

<%@page import="shop.ProductBean"%>
<%@page import="java.util.Enumeration"%>
<%@page import="shop.UtilMgr"%>
<%@page import="java.util.Hashtable"%>
<%@page import="shop.OrderBean"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="cMgr" scope="session" class="shop.CartMgr"/>
<jsp:useBean id="pMgr" class="shop.ProductMgr"/>
<%
	if(session.getAttribute("idKey")==null){
		response.sendRedirect("login.jsp");
		return; // 이후에 코드를 무력화
	}
	Hashtable<Integer, OrderBean> hCart= cMgr.getCartList();
	int totalPrice = 0;
	
%>
<html>
<head>
<title>Simple Shopping Mall</title>
<script src="script.js"></script>
</head>
<body bgcolor="#996600" topmargin="100">
	<%@ include file="top.jsp" %>
	<table width="75%" align="center" bgcolor="#FFFF99">
	<tr>
	<td align="center" bgcolor="#FFFFCC">
		<table width="95%" align="center" bgcolor="#FFFF99" border="1">
			<tr align="center" bgcolor="#996600">
				<td><font color="#FFFFFF">제품</font></td>
				<td><font color="#FFFFFF">수량</font></td>
				<td><font color="#FFFFFF">가격</font></td>
				<td><font color="#FFFFFF">수정/삭제</font></td>
				<td><font color="#FFFFFF">조회</font></td>
			</tr>
			
			<%if(hCart.isEmpty()){%>
			<tr>
				<td colspan="5" align="center">장바구니는 비어 있습니다.</td>
			</tr>
			<%}else{
				// Hashtable 에서 저장된 키 값을 리턴
				Enumeration<Integer> hCartKey = hCart.keys();	
				while(hCartKey.hasMoreElements()){
					// 키 값으로 주문객체를 리턴
					OrderBean order = hCart.get(hCartKey.nextElement());
					int productNo = order.getProductNo();
					// 상품정보를 리턴 
					ProductBean pbean = pMgr.getProduct(productNo);
					String pName = pbean.getName();
					int price = pbean.getPrice();
					int quantity = order.getQuantity();
					int subTotal = price * quantity; // 주문가격
					// 전체 주문 가격
					totalPrice += subTotal;
			%>
			<tr align="center">
			<form method="post" action="cartProc.jsp">
				<input type="hidden" name="productNo" 
				value="<%=productNo%>">
				<td><%=pName%></td>
				<td><input name="quantity" style = "text-align:right;" 
					value="<%=quantity%>" size="3"></td>
				<td><%=UtilMgr.monFormat(subTotal)%></td>
				<td>
					<input type="button" value="수정" size="3" onclick="javascript:cartUpdate(this.form)"> /
					<input type="button" value="삭제" size="3" onclick="javascript:cartDelete(this.form)">
				</td>
				<td>
					<a href="javascript:productDetail('<%=productNo%>')">상세보기</a>
				</td>
				<input type="hidden" name="flag">
				</form>
			</tr>		 
			<%	} // --while%>
			<tr>
				<td colspan="4" align="right">
					총 주문금액 : <%=UtilMgr.monFormat(totalPrice)%></td>
				<td align="center">
					<a href="orderProc.jsp">주문하기</a>
				</td>
			</tr>
		</table>
	</td>
	</tr>
	<%}// if~else %>
	</table>
	<%@ include file="bottom.jsp" %>
	<form name="detail" method="post" action="productDetail.jsp" >
		<input type="hidden" name="no">
	</form>	
</body>
</html>

OrderMgr.java

package shop;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class OrderMgr {
	private DBConnectionMgr pool;
	
	public OrderMgr() {
		pool = DBConnectionMgr.getInstance();
	}
	
	// Order Insert 
	//접수중(1),접수(2),입금확인(3),배송준비(4),배송중(5),완료(6)
	public void insertOrder(OrderBean order) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = pool.getConnection();
			sql = "insert tblOrder(id,productNo,quantity,date,state)"
					+ "values(?,?,?,?,?)";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, order.getId());//누가
			pstmt.setInt(2, order.getProductNo());//어떤 상품
			pstmt.setInt(3, order.getQuantity());//몇개
			pstmt.setString(4, UtilMgr.getDay());//언제
			pstmt.setString(5, "1");//상태 -> 접수중
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.freeConnection(con, pstmt);
		}
	}
	
	// Order List
	public Vector<OrderBean> getOrderList(String id){
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		Vector<OrderBean> vlist = new Vector<OrderBean>();
		try {
			con = pool.getConnection();
			sql = "select * from tblOrder where id = ? order by no desc";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				OrderBean order = new OrderBean();
				order.setNo(rs.getInt("no"));//주문번호
				order.setId(rs.getString("id"));//주문한 id
				order.setProductNo(rs.getInt("productNo"));//상품번호
				order.setQuantity(rs.getInt("quantity"));//주문수량
				order.setDate(rs.getString("date"));//주문날짜
				order.setState(rs.getString("state"));//주문상태
				vlist.addElement(order);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.freeConnection(con, pstmt, rs);
		}
		return vlist;
	}
	
	// admin mode
	// Order All List
	// Order All List
	// Order Detail
	// Order Update
	// Order Delete
}

orderProc.jsp

<%@page import="java.util.Enumeration"%>
<%@page import="shop.OrderBean"%>
<%@page import="java.util.Hashtable"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="cMgr" scope="session" class="shop.CartMgr"/>
<jsp:useBean id="pMgr" class="shop.ProductMgr"/>
<jsp:useBean id="orderMgr" class="shop.OrderMgr"/>
<%
	// 세션에 저장된 장바구니 리턴
	Hashtable<Integer, OrderBean> hCart = cMgr.getCartList();
	Enumeration<Integer> hCartKey = hCart.keys();
	String msg = "";
	if(!hCart.isEmpty()){
		while(hCartKey.hasMoreElements()){
			OrderBean order = hCart.get(hCartKey.nextElement());
			// 주문 처리
			orderMgr.insertOrder(order);
			// 재고 정리
			pMgr.reduceProduct(order);
			// 장바구니에 주문한 상품 삭제
			cMgr.deleteCart(order);
		} // --while
		msg = "주문처리 하였습니다.";
	} else {
		msg = "장바구니가 비었습니다.";
	}
%>
<script>
	alert("<%=msg%>");
	location.href = "orderList.jsp";
</script>

결과 화면





페이지 통신에 한글이 안 깨지는 이유

1.get : Sever.xml (63라인) 세팅

  • 기본값이 URIEncoding="UTF-8"

2.post : web.xml 세팅

  • filter, filter-mapping 태그

0개의 댓글

관련 채용 정보