728x90
4-2. 장바구니
main.html
<!DOCTYPE html>
<html lang="ko">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<h2>메인</h2>
<a href="listProd.jsp"><button type="button">상품목록</button></a>
<a href="cartProc.jsp"><button type="button">장바구니</button></a>
</body>
</html>
listProd.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">
<h2>상품 목록</h2>
<form action="cartProc.jsp" method="post">
<ul>
<li>노트북<input type="checkbox" name="wishProd" value="노트북"></li>
<li>전기차<input type="checkbox" name="wishProd" value="전기차"></li>
<li>아파트<input type="checkbox" name="wishProd" value="아파트"></li>
<li>북한산<input type="checkbox" name="wishProd" value="북한산"></li>
</ul>
<a href="main.html"><button type="button">메인으로</button></a>
<button type="submit">장바구니에 담기</button>
</form>
cartProc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder, java.net.URLDecoder"%>
<%@ page import="java.util.StringTokenizer"%>
<%@ page import="java.util.HashSet, java.util.Set"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("utf-8");
//찜한 상품들
String[] wishProds = request.getParameterValues("wishProd");
//장바구니에서 삭제한 상품들
String[] delProds = request.getParameterValues("delProd");
//장바구니
Set<String> cart = new HashSet<>();
//장바구니에 상품들을 담는다.
Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
if(cookie.getName().equals("cart")){
String tmp = URLDecoder.decode(cookie.getValue(), "utf-8");
StringTokenizer st = new StringTokenizer(tmp, ",");
while(st.hasMoreTokens()) cart.add(st.nextToken());
}
}
//상품을 찜하면
if(wishProds != null)
for(String prod:wishProds) cart.add(prod);
//장바구니에서 상품을 삭제하면
if(delProds != null)
for(String prod:delProds) cart.remove(prod);
//장바구니를 client에 전달한다.
String cartStr="";
for(String prod:cart) cartStr += prod+",";
cartStr = URLEncoder.encode(cartStr,"utf-8");
Cookie cookie = new Cookie("cart", cartStr);
cookie.setMaxAge(60*60*24*365);
response.addCookie(cookie);
request.setAttribute("cart", cart);
%>
<jsp:forward page="cartOut.jsp"/>
cartOut.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.HashSet, java.util.Set"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<h2>장바구니</h2>
<form action="cartProc.jsp" method="post">
<ul>
<%
Set<String> cart = (Set<String>)request.getAttribute("cart");
if(cart.size()==0){
%>
<p>장바구니가 비었습니다</p>
<%
}else{
for(String prod:cart){
%>
<li><%=prod%>
<input type="checkbox" name="delProd" value="<%=prod %>">
</li>
<%
}
}
%>
</ul>
<a href="listProd.jsp"><button type="button">상폼목록</button></a>
<button type="submit">삭제</button>
</form>
728x90
'WEB > JSP' 카테고리의 다른 글
[JSP] ch04-01. Cookie (0) | 2017.11.15 |
---|---|
[JSP] ch03-07. Include3 (0) | 2017.11.14 |
[JSP] ch03-06. Scope (0) | 2017.11.14 |
[JSP] ch03-05. Forward Login (0) | 2017.11.13 |
[JSP] ch03-04. Redirect Login (0) | 2017.11.13 |
댓글