프로젝트/프로그램
JSP 장바구니 기능 구현
GaeGim
2022. 9. 20. 19:48
반응형
Login.jsp
-로그인 화면.
비밀번호 입력은 없으며, 사용자 이름을 입력하는 양식만을 제공
사용자 이름 미입력 시 알람
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>GABALMARKET</title>
<!-- 세션 초기화 -->
<%
session.invalidate();
%>
</head>
<body align="center">
<h1>SIGN IN</h1><hr><br>
<form method=post action=setProducts.jsp>
<table align="center">
<tr><td align="left"><p style="font-size:0.8em;"><B>NAME</B></p><td></tr>
<tr><td><input type="text" name="username" size="10"></td></tr>
<tr><td><br><br><input type="submit" value="LogIn"><td></tr>
</table>
</form>
</body>
</html>
setProducts.jsp
-상품 선택 화면.
상품 리스트에서 원하는 상품을 선택하고 장바구니에 추가하는 버튼이 있다.
추가하지 않은 경우 비어있음
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: center;
}
</style>
<meta charset="EUC-KR">
<title>GABALMARKET</title>
<!-- 세션에 아이디 넣기 -->
<%
request.setCharacterEncoding("EUC-KR");
%>
<%
String username = request.getParameter("username");
session.setAttribute("session_id", username);
%>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username");
session.setAttribute("username", name);
System.out.println(name);
if(session.getAttribute("username") == ""){
%> <script>
alert('ENTER THE NAME')
history.go(-1) </script>
<%
}
%>
</head>
<body>
<h1>PRODUCTS LIST</h1>
<hr>
<B><%=request.getParameter("username")%></B> BABO~~
<hr>
<h3>FRUITS</h3>
<form>
<select name="FRUITS">
<option value="APPLE">APPLE</option>
<option value="CHERRY">CHERRY</option>
<option value="MELON">MELON</option>
<option value="BABO">BABO</option>
<option value="ORANGE">ORANGE</option>
</select> <input type="submit" name="ADD" value="ADD" formaction="add.jsp">
<br> <br> <br>
<input type="submit" name="BUY" value="BUY" formaction="checkOut.jsp">
</form>
</body>
</html>
add.jsp
- setProducts.jsp에서 선택한 상품을 세션에 넣는다.
선택된 데이터를 모두 저장해야 하므로 ArrayList를 이용한다. 상품이 추가되었다는 메세지를 보여주고 다시 setProducts.jsp 화면으로 돌아간다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<%
String fruit = request.getParameter("FRUITS");
ArrayList<String> list = (ArrayList) session.getAttribute("list");
if (list == null) {
list = new ArrayList<String>();
}
if(fruit!=null){
if (!list.contains(fruit)) {
session.setAttribute(fruit, 1);
list.add(fruit);
} else {
int cnt = (Integer) session.getAttribute(fruit) + 1;
session.setAttribute(fruit, cnt);
}
}
session.setAttribute("list", list);
%>
</head>
<body>
</body>
<script>
alert("<%=fruit%> is added");
/* 뒤로 가기 */
history.go(-1);
</script>
</html>
checkOut.jsp
- 세션이 살아 있고, 하나 이상의 상품을 선택한 상태라면 선택한 상품의 목록을 보여준다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<style>
img {
display: block;
margin: auto;
text-align: center;
}
body {
text-align: center;
}
</style>
<meta charset="EUC-KR">
<title>GABALMARKET</title>
<%
request.setCharacterEncoding("utf-8");
ArrayList<String> list = (ArrayList) session.getAttribute("list");
String removeItem = request.getParameter("removeItem");
if (removeItem != null) {
list.removeAll(Arrays.asList(removeItem));
}
%>
</head>
<body>
<h1>SHOPPING CART</h1>
<form method=post>
<hr>
<br>
<%
if (list == null) {
%><h3>Empty</h3><br>
<%
} else {
Set<String> listSet = new HashSet<String>(list);
for (String s : listSet) {
out.println(s + " " + session.getAttribute(s) + "개");
%><br>
<%
}
}
%><br> <br>
<button type="button" style="padding: 8px 10px 7px 8px" name="delete" onclick="location.href='del.jsp'">
<img src="bin.png" width="17" height="18">
</button>
<button type="submit" style="padding: 8px 10px 7px 8px" name="toList" formaction="setProducts.jsp">
<img src="cart.png" width="17" height="18">
</button><br><br>
<input type="submit" value="LogOut" name="toLogin" formaction="Login.jsp">
</form>
</body>
</html>
-로그아웃
반응형