생각
생각해 보자. 구매 확정으로 가는 단계 두 가지
1. 장바구니 -> 장바구니에 있는 cartList 구매 확정 페이지로 보내기 -> 구매 확정 페이지로 보내지면 장바구니에 있는 목록 삭제
2. 개별 상품 구매 -> 상품 구매 페이지 띄우기
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- mall_order.jsp -->
<jsp:useBean id="productList" class="my.shop.mall.ProductList" scope="session"/>
<jsp:useBean id="cart" class="my.shop.mall.CartBean" scope="session"/>
<%
String pnum = request.getParameter("pnum");
String select = request.getParameter("select");
String pqty = request.getParameter("pqty");
List<ProductDTO> plist = null;
if (pnum == null){ //장바구니에서 넘어옴
plist = cart.getListCart();
}else { //즉시구매에서 넘어옴
ProductDTO getPDTO = productList.getProduct(Integer.parseInt(pnum), select);
getPDTO.setPqty(Integer.parseInt(pqty));
plist = new ArrayList<>();
plist.add(getPDTO);
}
java.text.DecimalFormat df = new java.text.DecimalFormat("###,###");
int totalPrice = 0;
%>
<%@ include file="mall_top.jsp" %>
<div align="center">
<table border="0" class="outline" width="50%">
<tr>
<th colspan="4" class="m2"><h3>결재 내역서 보기</h3></th>
</tr>
<tr>
<th class="m1" width="40%">상품명</th>
<th class="m1" width="10%">수량</th>
<th class="m1" width="25%">단가</th>
<th class="m1" width="25%">금액</th>
</tr>
<% for(ProductDTO pdto : plist){ %>
<tr>
<td align="center"><%=pdto.getPname()%></td>
<td align="right"><%=pdto.getPqty()%>개</td>
<td align="right"><%=df.format(pdto.getPrice())%>원</td>
<td align="right"><%=df.format(pdto.getPrice()*pdto.getPqty())%>원</td>
</tr>
<% totalPrice += pdto.getPrice() * pdto.getPqty();
}
cart.clearCart();
%>
<tr>
<td colspan="4" align="center" class="m1">
<b>결재하실 총액은 : </b><font color="red"><%=totalPrice%>원</font>
</td>
</tr>
</table>
</div>
<%@ include file="mall_bottom.jsp"%>
⭐
JSP Life Cycle
JSP의 생명주기는 JSP 페이지가 클라이언트 요청을 처리하기 위해 생성되고 실행되는 과정을 설명하는 것이다. JSP 페이지는 웹 애플리케이션에서 동적인 웹 페이지를 생성하기 위해 사용되며, 이러한 페이지의 생명주기는 3단계로 나뉜다.
jsp파일이 실행될때 제일 먼저 불려가서 실행되는 메소드
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.io.*"%>
<!-- count.jsp -->
<%!
int count = 0; //방문객 수를 넣기 위한 변수
String path = null; //방문객 수를 저장할 파일의 위치를 알려주는 변수
File file = null;
public void jspInit(){
path = this.getServletContext().getRealPath("/count/count.txt");
FileReader fr = null;
BufferedReader br = null;
try {
File file = new File(path);
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = br.readLine();
if (str != null){
count = Integer.parseInt(str);
}
}catch(FileNotFoundException e){
e.printStackTrace();
countSaveFile(); // 저장 메서드
}catch(IOException e){
e.printStackTrace();
countSaveFile();
}
}
public void countSaveFile(){
FileWriter fw;
PrintWriter pw;
try {
File file = new File(path);
fw = new FileWriter(file);
pw = new PrintWriter(fw, true);
pw.println(count);
pw.close();
fw.close();
}catch(IOException e){
e.printStackTrace();
}
}
init메소드 실행이 된후, 또는 없을때 불려가서 실행되는 메소드
파일 종료시 불려가서 실행되는 메소드
public void jspDestroy(){
countSaveFile();
}
%>
<%
if (session.isNew()) { // 새로운 session 이 들어왔을 때 == 새로운 사용자가 들어왔을 때
count++;
}
if (count%10 == 0){
countSaveFile();
}
%>
<b>방문자 수 :
<%
String countStr = String.valueOf(count);
int len = countStr.length();
for (int i = 0; i < len; ++i) {%>
<img src="<%=request.getContextPath()%>/img/no<%=countStr.charAt(i)%>.gif">
<% }%>
명</b>
<body>
<div align="center">
<table border="1" width="70%" height="90%">
<tr height="10%">
<td colspan="2" align="center">
<a href="<%=request.getContextPath()%>/index.jsp">HOME</a> |
<% if (isLogin){ %>
<a href="<%=request.getContextPath()%>/login/logout.jsp">로그아웃</a> |
<% }else { %>
<a href="<%=request.getContextPath()%>/login/login.jsp">로그인</a> |
<% } %>
<a href="javascript:checkMember()">회원가입</a> |
<a href="<%=request.getContextPath()%>/member/memberAll.jsp">회원보기</a> |
<a href="<%=request.getContextPath()%>/member/memberAll.jsp?mode=find">회원찾기</a> |
<a href="<%=request.getContextPath()%>/board/list.jsp">게시판</a> |
<a href="<%=request.getContextPath()%>/company.jsp">회사소개 </a>
</td>
</tr>
<tr height="80%">
<td width ="20%" align="center" valign="top">
<% if (isLogin){%>
<%=id%> 님, 환영합니다.
<% }else{%>
로그인을 해 주세요.
<% }
%>
<jsp:include page="/count/count.jsp"/>
</td>
<td width="80%">