WEB/JSP

[JSP] 간단한 쇼핑몰 장바구니

다콩잉 2022. 9. 20. 14:46

이번에는 jsp로 간단한 쇼핑몰 장바구니 기능을 구현해봤다. 아주아주 간단한 페이지이당..!

 

로그인 페이지
상품 선택 페이지
장바구니 페이지

 

 

기능

  • 로그인
  • 로그아웃
  • 상품 선택
  • 선택상품 확인
  • 상품 수량 변경
  • 선택상품 삭제
  • 30초동안 액션이 없으면 자동 로그아웃

 

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 9:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String name = (String)session.getAttribute("name");
    if(name != null){
        response.sendRedirect("setProduct.jsp");
    }
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <center>
        <h2>로그인</h2>
        <hr>
        <form action="setProduct.jsp">
            <input type="text" name="name" placeholder="이름" required>
            <input type="submit" value="로그인">
        </form>
    </center>
</body>
</html>

logout.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Enumeration" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <%
    session.invalidate();
    response.sendRedirect("login.jsp");
  %>
</body>
</html>

setProduct.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 9:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String name = request.getParameter("name");
    if(name != null){
        if(session.getAttribute("name") == null){
            session.setAttribute("name", name);
        }
    }else{
        if(session.getAttribute("name")== null){
            response.sendRedirect("login.jsp");
        }else{
            name = (String)session.getAttribute("name");
        }
    }
    session.setMaxInactiveInterval(30); // 초 단위
%>
<center>
    <h2>상품선택</h2>
    <hr>
    <h3><%=name%>님이 로그인 한 상태 입니다.</h3>
    <hr>
    <form action="add.jsp">
        <select name="product">
            <option value="사과">사과</option>
            <option value="귤">귤</option>
            <option value="파인애플">파인애플</option>
            <option value="자몽">자몽</option>
            <option value="레몬">레몬</option>
        </select>
        <input type="submit" value="추가">
    </form>
    <a href="checkOut.jsp">계산</a>
    <a href="logout.jsp">로그아웃</a>
</center>
</body>
</html>

add.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 9:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.ArrayList" %>
<%
    String name = (String)session.getAttribute("name");
    if(name == null){
        response.sendRedirect("login.jsp");
    }


    String product = request.getParameter("product");
    if(session.getAttribute("product") == null) {
        ArrayList<String> arr = new ArrayList<String>();
        arr.add(product);
        session.setAttribute(product, 1);
        session.setAttribute("product", arr);
    }else{
        ArrayList<String> arr = (ArrayList<String>) session.getAttribute("product");
        if(!arr.contains(product)){arr.add(product); session.setAttribute(product, 1);}
        else{
            int count = (int)session.getAttribute(product);
            session.setAttribute(product, count+1);
        }
        session.setAttribute("product", arr);
    }
%>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript">
        const product = "<%=product%>";
        alert(product + "가 추가 되었습니다!!");
        location.href = "setProduct.jsp";
    </script>
</head>
<body>

</body>
</html>

checkOut.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 9:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<%
    String name = (String)session.getAttribute("name");
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <center>
        <h2><%=name%>님의 장바구니</h2>
        <h3>선택한 상품 목록</h3>
        <hr>
        <%
            if(name == null){
                response.sendRedirect("login.jsp");
            }
            if(session.getAttribute("product") != null){
                ArrayList<String> arr = (ArrayList<String>) session.getAttribute("product");
                for(int i = 0; i < arr.size(); i++) {
                    int count = (int) session.getAttribute(arr.get(i));
                    String value = arr.get(i) + "-" + count + "개";
        %>
                    <h3><%=value%></h3>
                    <button type="button"><a href="remove.jsp?val=<%=arr.get(i)%>&btn=add">+</a></button>
                    <button type="button"><a href="remove.jsp?val=<%=arr.get(i)%>&btn=-">-</a></button>
                    <button type="button"><a href="remove.jsp?val=<%=arr.get(i)%>&btn=x">X</a></button>
        <%
                }
            }
        %>
        <br>
        <button><a href="setProduct.jsp">목록</a></button>
    </center>
</body>
</html>

remove.jsp

<%--
  Created by IntelliJ IDEA.
  User: UserK
  Date: 2022-09-20
  Time: 오전 11:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.ArrayList" %>
<%
    String name = (String)session.getAttribute("name");
    if(name == null){
        response.sendRedirect("login.jsp");
    }

    String productName = request.getParameter("val");
    String btn = request.getParameter("btn");
    ArrayList<String> arr = (ArrayList<String>) session.getAttribute("product");
    int cnt = (int)session.getAttribute(productName);
    if(btn.equals("add")){
        session.setAttribute(productName, cnt+1);
    }else if(btn.equals("-")){
        session.setAttribute(productName, cnt-1);
    }else if(btn.equals("x")){
        arr.remove(productName);
        session.setAttribute("product", arr);
    }
    response.sendRedirect("checkOut.jsp");
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
</body>
</html>
728x90