본문으로 바로가기

[JSP] 쇼핑몰 장바구니 기능 (DB연동 x)

category Back-End/JSP 2018. 4. 13. 19:35

1. 로그인 폼 만들기 ( Login.jsp)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@page import="java.util.ArrayList"%>
<%@ 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>로그인 화면</title>
</head>
<body>
<center><H2>로 그 인</H2>
<HR>
<% session.invalidate(); %>
<form name="login" method="post" action="setProduct.jsp">
아이디 입력 : <input type="text" name="username">
<input type="submit" value="로그인">
</form>
</center>
</body>
</html>

cs


13번행 : 다시 로그인 화면으로 돌아왔을 때, 기존의 세션 scope를 종료시켜준다.




2. 상품 선택 페이지 만들기 ( setProduct.jsp)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<%@page import="java.util.ArrayList"%>
<%@ 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>상품 선택 화면</title>
</head>
<body>
<center><H2>상품 선택하기</H2>
<% request.setCharacterEncoding("UTF-8"); %>
<%
    session.setAttribute("username", request.getParameter("username"));
%>
<% if(request.getParameter("username")=="") { %>
<script>alert("아이디를 입력하세요"); history.back(); </script>
<% } %>
<%= session.getAttribute("username"%>님이 로그인 한 상태입니다. 반갑습니다^^<HR><br>
<form name="set" action="add.jsp" method="post">
<select name="product"><option>사과</option>
                       <option>바나나</option>
                       <option>배</option>
                          <option>포도</option>
                       <option>귤</option></select>
<input type="submit" value="추가"><br><br>
</form>
<form name="go" action="checkOut.jsp" method="post">
<input type="submit" value="장바구니 가기"><br><br>
</form>
<form name="reset" action="Login.jsp" method="post">
<input type="submit" value="로그아웃">
</form>
</center>
</body>
</html>
cs



로그인 페이지에서 입력한 이름 값을 request scope로 넘겨받고,


넘겨받은 값을 세션 scope에 저장한다.




3. 선택한 상품 추가하는 페이지 (add.jsp)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8"); %>
<% String product = request.getParameter("product"); %>
<% ArrayList<String> arr = (ArrayList)session.getAttribute("arr"); %>
<% 
    if(arr==null) {
        arr = new ArrayList<String>();
    }
    arr.add(product);
%>
<% session.setAttribute("arr", arr); %>
 
 
<script>alert("<%= product %>이(가) 추가되었습니다"); history.back();</script>
</body>
</html>
cs


선택한 상품을 ArrayList에 저장하고 업데이트 된 ArrayList를 session scope에 다시 저장한다.


ArrayList를 저장하고 history.back()메서드를 이용해 다시 이전 페이지로 되돌아간다.




4. 세션에 저장된 ArrayList 출력하는 페이지 (checkOut.jsp)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>저장된 상품들 출력 화면</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8"); %>
<% String name = (String)session.getAttribute("username"); %>
<% ArrayList<String> arr = (ArrayList)session.getAttribute("arr"); %>
<center>
<H2>장바구니</H2>
<br>
<%= name %>님께서 고른 상품 목록
<HR>
 
<%
    if(arr==null) {
%> 장바구니에 담은 상품이 없습니다.
 
<%
    } else {
    for(String i : arr) {
        out.println(i);
        }
    }%>
<br><br>
<form name="dd" action="Login.jsp" method="post">
<table>
    <tr>
        <td><input type="button" value="뒤로 가기" onClick="history.back()"></td>
        <td><input type="submit" value="로그아웃"></table>
</form>
</center>
</body>
</html>
cs



상품 선택 페이지에서 장바구니 보기를 선택하면 ArrayList에 저장되어 있는 상품 값들이 출력된다.


뒤로 가기를 누르면 상품선택 페이지로 돌아가고 로그아웃을 누르면 로그인 페이지로 돌아 간다.