JSP 쇼핑몰 부트스트랩 적용

별의개발자커비·2023년 5월 20일
0

JSP

목록 보기
16/31
post-thumbnail

1. 배송 정보 등 만들기 (쿠키에다 저장) 개선 ver. 3

01. cart.jsp

<%@page import="dao.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link
	href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
	rel="stylesheet"
	integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
	crossorigin="anonymous">
<title>cart</title>
</head>
<body>
<jsp:include page="./menu.jsp"></jsp:include>
	<div class="container">
		<h1 class="display-3">장바구니</h1>
	</div>
	
	<%
		List<Product> cartlists = ( List<Product>) session.getAttribute("cartlist");
	%>
	
	<div class="container">
		<div class="row">
			<table width= 100% >
				<tr> <!---->
					<td align="left"><a href="#" class="btn btn-danger">삭제하기</a></td>
					<td align="right"><a href="./shippinginfo.jsp?cartid=<%=cartlists%>" class="badge bg-success">주문하기</a></td>
				</tr> 
				
			</table>
		</div>
		
		<div style="padding-top: 50px">
			<table class="table table-hover">
				<tr>
					<th>상품</th> <!--  열의 헤더 -->
					<th>가격</th>
					<th>수량</th>
					<th>소계</th>
					<th>비고</th>
				</tr>
				<%
				if(cartlists== null){
					return;
				}
				int sum = 0;
				for( Product p : cartlists ){
					sum += p.getUnitPrice();
				%>
				<tr>
					<td><%= p.getProductId()%></td>
					<td><%= p.getUnitPrice()%></td>
					<td><%= p.getUnitInStock()%></td>
					<td><%= p.getQuantity()%></td>
					<td><a href="#" class="badge bg-danger">삭제</a></td>
				</tr>
				<% } %>
				<tr>
					<td></td>
					<td></td>
					<td></td>
					<th>총액</th>
					<th><%= sum %></th>
				</tr>
			</table>
			<a href="products.jsp" class="btn btn-secondary">쇼핑 계속하기</a>
		</div>
	</div>
	
	
</body>
</html>

- getId() 로 가져올 수 있겠구나!

	String cartId = session.getId();  
				<td align="left"><a href="./deleteCart.jsp?cartId=<%=cartId%>" class="btn btn-danger">삭제하기</a></td>
                					<td align="right"><a href="./shippinginfo.jsp?cartId=<%=cartId%>" class="btn btn-success">주문하기</a></td>

02. shippinginfo.jsp 배송정보

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>shippinginfo 배송정보</title>
<link
	href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
	rel="stylesheet"
	integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
	crossorigin="anonymous">
</head>
<body>
	<jsp:include page="./menu.jsp"></jsp:include>
	<div class="container">
		<h1 class="display-3">배송정보</h1>
	</div>
	
	<div class="container">
		<form action="./processShippingInfo.jsp" class="form-horizontal" method="post">
			<input type="hidden" name="cartId" value="<%= request.getParameter("cartId") %>">
			
			<div class="form-group row">
				<label class="col-sm-2">성명</label>
				<div class="col-sm-3">
					<input name="name" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">배송일</label>
				<div class="col-sm-3">
					<input name="shippingDate" type="text" class="form-control">(yyyy/mm/dd)
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">국가명</label>
				<div class="col-sm-3">
					<input name="country" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">우편번호</label>
				<div class="col-sm-3">
					<input name="zipCode" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">주소</label>
				<div class="col-sm-3">
					<input name="addressName" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
				
					<a href="./cart.jsp?cartid=<%=request.getParameter("cartId")%>" class="btn btn-secondary" role="button">이전</a> 
					
					<input type="submit" class="btn btn-primary" value="등록">
					
					<a href="./checkoutcancelled.jsp" class="btn btn-secondary" role="button">취소</a> 
					
				</div>
			</div>
		</form>
	</div>
	

</body>
</html>

03. processShippingInfo.jsp 배송처리

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta charset="UTF-8">
<%!
	Cookie makeCookie(String parameter, ServletRequest request)  throws Exception {
	return new Cookie( parameter , URLEncoder.encode( request.getParameter(parameter), "utf-8" ) );
}
%>
<%
	response.addCookie(makeCookie("cartId", request)) ;
	response.addCookie(makeCookie("name", request));
	response.addCookie(makeCookie("shippingDate", request));
	response.addCookie(makeCookie("country", request));
	response.addCookie(makeCookie("zipCode", request));
	response.addCookie(makeCookie("addressName", request));
	
	response.sendRedirect("orderConfirmation.jsp");
%>
  • @@ utf 왜 필요? 필터 적용은?
  • // response.addCookie(makeCookie("cartId", request)); // 이게 왜 필요??

04. orderConfirmation.jsp 주문확인

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	String cartId = session.getId();
	String name = "", shippingDate= "" , country= "", zipCode= "", addressName= "" ; 
	String [] params = {  "name",  "shippingDate", "country" ,  "zipCode" ,  "addressName"};
	Cookie [] cookies = request.getCookies();
	for(int i = 0 ; i < params.length; i++ ){
		if(cookies[i].getName().trim().equals("name")){
			name = URLDecoder.decode(cookies[i].getValue(), "utf-8");
		} else if (cookies[i].getName().trim().equals("shippingDate")){
			shippingDate = URLDecoder.decode(cookies[i].getValue(), "utf-8");
		} else if (cookies[i].getName().trim().equals("country")){
			country = URLDecoder.decode(cookies[i].getValue(), "utf-8");
		} else if (cookies[i].getName().trim().equals("zipCode")){
			zipCode = URLDecoder.decode(cookies[i].getValue(), "utf-8");
		} else if (cookies[i].getName().trim().equals("addressName")){
			addressName = URLDecoder.decode(cookies[i].getValue(), "utf-8");
		}
		
		/* if(cookies[i].getName().trim().equals(params[i])){
			name =
		} */
	}
		
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>orderConfirmation 주문정보</title>
<link
	href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
	rel="stylesheet"
	integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
	crossorigin="anonymous">
</head>
<body>
	<jsp:include page="./menu.jsp"></jsp:include>
	<div class="container">
		<h1 class="display-3">주문정보</h1>
	</div>
	
	<div class="container">
		
			<input type="hidden" name="cartId" value="<%= request.getParameter("cartId") %>">
			
			<div class="form-group row">
				<label class="col-sm-2">성명</label>
				<div class="col-sm-3">
					<input disabled="enabled"  value="<%=name%>" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">배송일</label>
				<div class="col-sm-3">
					<input disabled="enabled"  value="<%=shippingDate%>" type="text" class="form-control">(yyyy/mm/dd)
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">국가명</label>
				<div class="col-sm-3">
					<input disabled="enabled"  value="<%=country%>" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">우편번호</label>
				<div class="col-sm-3">
					<input disabled="enabled"  value="<%=zipCode%>" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<label class="col-sm-2">주소</label>
				<div class="col-sm-3">
					<input disabled="enabled"  value="<%=addressName%>" type="text" class="form-control">
				</div>
			</div>
			
			<div class="form-group row">
				<div class="col-sm-offset-2 col-sm-10">
				
					<a href="./cart.jsp?cartid=<%=request.getParameter("cartId")%>" class="btn btn-secondary" role="button">이전</a> 
					
					<a href="./checkoutcancelled.jsp" class="btn btn-primary" role="button">주문</a> 
					
					<a href="./checkoutcancelled.jsp" class="btn btn-secondary" role="button">취소</a> 
					
				</div>
			</div>
	</div>
	


</body>
</html>

내 버전

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String cartId = session.getId();
	String name = "", shippingDate = "", country = "",  zipCode= "", addressName= "";
	
	// String name, shippingDate, country, zipCode, addressName = "";
	Cookie[] cookies = request.getCookies();
	for ( Cookie co : cookies ) {
		if(co.getName().equals("name")){
			name = co.getValue();		
		} else if(co.getName().equals("shippingDate")){
			shippingDate = co.getValue();	
		} else if(co.getName().equals("country")){
			country = co.getValue();	
		} else if(co.getName().equals("zipCode")){
			zipCode = co.getValue();	
		} else if(co.getName().equals("addressName")){
			addressName = co.getValue();	
		}
	}

%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>orderConfirmation 주문정보</title>
<link
	href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
	rel="stylesheet"
	integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
	crossorigin="anonymous">
</head>
<body>
	<jsp:include page="./menu.jsp"></jsp:include>
	<div class="container">
		<h1 class="display-3">주문정보</h1>
	</div>
	
	<div class="container">
		 <%-- <input type="hidden" name="cartId" value="<%= request.getParameter("cartId") %>"> --%>
		 <div class="form-group row">
		 	<label class="col-sm-2">성명</label>
		 	<div class="col-sm-3">
		 		<input disabled="disabled" value="<%=name %>" type="text" class="form-control"> 
		 	</div>
		 </div>
		
		 <div class="form-group row">
		 	<label class="col-sm-2">배송일</label>
		 	<div class="col-sm-3">
		 		<input disabled="disabled" value="<%=shippingDate %>" type="text" class="form-control"> (yyyy/mm/dd)
		 	</div>
		 </div>
		
		 <div class="form-group row">
		 	<label class="col-sm-2">국가명</label>
		 	<div class="col-sm-3">
		 		<input disabled="disabled" value="<%=country %>" type="text" class="form-control"> 
		 	</div>
		 </div>
		
		 <div class="form-group row">
		 	<label class="col-sm-2">우편번호</label>
		 	<div class="col-sm-3">
		 		<input disabled="disabled" value="<%=zipCode %>" type="text" class="form-control"> 
		 	</div>
		 </div>
		
		 <div class="form-group row">
		 	<label class="col-sm-2">주소</label>
		 	<div class="col-sm-3">
		 		<input disabled="disabled" value="<%=addressName %>" type="text" class="form-control"> 
		 	</div>
		 </div>
		
		 <div class="form-group row">
		 
		 </div>
		
	</div>

</body>
</html>

2. 다 새로 받고 새로 시작

1. LogFileFilter 여기 뭐 추가했는데??








2. LogFilter.jsp

package filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class LogFilter implements Filter  {
	
	public void  init(FilterConfig config) throws ServletException{
		System.out.println("WebMarket...."); 
	}
	
	public void  doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws java.io.IOException, ServletException {
		System.out.println("IP : " + request.getRemoteAddr());
		long start = System.currentTimeMillis();
        System.out.println("  URL  : " + getURLPath(request));
		System.out.println("현재시간 : " + getCurrentTime());
		chain.doFilter(request,response);
		
		long end = System.currentTimeMillis();		
		System.out.println(" 현재시간: " + getCurrentTime());
		System.out.println("처리시간 : " + (end-start)+ "ms ");
		System.out.println("=======================================================");
	}

	public void destroy( ){
      
	}

	private String getURLPath(ServletRequest request) {
		HttpServletRequest req;
		String currentPath="";
		String queryString=""; 
		if(request instanceof HttpServletRequest){
			req = (HttpServletRequest)request;
			currentPath = req.getRequestURI();
			queryString = req.getQueryString();
			queryString = queryString == null ? "" : "?" + queryString;
		}
		return currentPath+queryString;
	}
	
	private String getCurrentTime() {
		DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return formatter.format(calendar.getTime());
	}
}

3. LogFileFilter.jsp

package filter;
// 여기 뭐 추가했는데??
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

public class LogFileFilter implements Filter {

	PrintWriter writer;

	public void init(FilterConfig config) throws ServletException {
		String filename = config.getInitParameter("filename");

		if (filename == null)
			throw new ServletException("filename null에러1이 발생했습니다.");

		try {
			writer = new PrintWriter(new FileWriter(filename, true), true);
		} catch (IOException e) {
			throw new ServletException("filename null에러2가 발생했습니다.");
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException {
		writer.println("  IP : " + request.getRemoteAddr());
		long start = System.currentTimeMillis();
		writer.println(" URL : " + getURLPath(request));
		writer.println(" 현재시간 :" + getCurrentTime());
		
		chain.doFilter(request, response);

		long end = System.currentTimeMillis();
		writer.println(" 현재시간 : " + getCurrentTime());
		writer.println(" 처리시간 : " + (end - start) + "ms ");
		writer.println("=======================================================");
	}

	public void destroy() {
		writer.close();
	}

	private String getURLPath(ServletRequest request) {
		HttpServletRequest req;
		String currentPath = "";
		String queryString = "";
		if (request instanceof HttpServletRequest) {
			req = (HttpServletRequest) request;
			currentPath = req.getRequestURI();
			queryString = req.getQueryString();
			queryString = queryString == null ? "" : "?" + queryString;
		}
		return currentPath + queryString;
	}

	private String getCurrentTime() {
		DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return formatter.format(calendar.getTime());
	}
}

3. db 연결하기

01. jar 다운, add jar

profile
재미없으면 안한다. 재밌으면 끝까지 한다😋

0개의 댓글