📃 내장객체 session을 이용한 장바구니
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ch06 : login.jsp</title>
</head>
<body>
<div align="center">
<H2>로그인</H2>
<form name="form1" method="POST" action="selProduct.jsp">
<input type="text" name="username"/>
<input type="submit" value="로그인"/>
</form>
</div>
</body>
</html>
selProduct.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
session.setAttribute("username", username);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>ch06 : selProduct.jsp</title>
</head>
<body>
<div align="center">
<H2>상품선택</H2>
<HR>
${username } 님 환영합니다!!!!
<HR>
<form name="form1" method="POST" action="add.jsp">
<SELECT name="fruit">
<option>사과</option>
<option>귤</option>
<option>파인애플</option>
<option>자몽</option>
<option>레몬</option>
</SELECT>
가격 : <input type="text" name="price"><br>
수량 : <input type="text" name="amount"><br>
<input type="submit" value="추가"/>
</form>
<a href="checkOut.jsp">계산</a>
</div>
</body>
</html>
add.jsp
<%@page import="kosta.bean.Product"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%! List<Product> list = new ArrayList<Product>(); %>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="product" class="kosta.bean.Product"/>
<jsp:setProperty property="*" name="product"/>
<%
list = (List)session.getAttribute("productlist");
if(list == null){
list = new ArrayList<Product>();
session.setAttribute("productlist", list);
}
list.add(product);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="javascript:history.back()">뒤로가기</a>
<%=product.getFruit() %>
</body>
</html>
checkOut.jsp
<%@page import="kosta.bean.Product"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
List<Product> list = (List)session.getAttribute("productlist");
/* if(list == null){
out.println("상품없음");
}else{
for(String fruit : list){
out.println(fruit + "<br>");
}
} */
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(list == null){
%>
<b>선택한 상품이 없습니다.</b>
<% }else { %>
<ul>
<%
int total = 0;
for(int i = 0; i < list.size(); i++) {
Product product = list.get(i);
total += (product.getPrice() * product.getAmount());
%>
<li><%= product.getFruit() %> : <%= product.getPrice() %> * <%= product.getAmount() %> = <%= (product.getPrice() * product.getAmount()) %></li>
<%} %>
</ul>
<br>
주문한 총 금액 : <%= total %> 원
<%} %>
</body>
</html>