JSP
(JSP) 장바구니
김만식이
2020. 8. 27. 18:31
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
</script>
</head>
<body>
<h1>로그인</h1>
<form action="Select.jsp" method="post">
<input type="text" name="info">
<input type="submit" value="로그인">
</form>
</body>
</html>
login
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%request.setCharacterEncoding("UTF-8");%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String info = request.getParameter("info");
session.setAttribute("info",info);
%>
<!--다음 페이지에 arraylist룰 사용하기위한 선언 -->
<%
ArrayList<String> arr = new ArrayList<>();
%>
<%session.setAttribute("arr",arr);%>
<h1>상품선택</h1>
<p><%=session.getAttribute("info")%>님 안녕하세요 <%=session.getAttribute("info")%></p>
<form method="post" action="Add.jsp">
<select name="fruit">
<option value="배">배</option>
<option value="사과">사과</option>
<option value="오렌지">오렌지</option>
<option value="귤">귤</option>
</select>
<input type="submit" value="추가" onclick="Add.jsp">
<a href="Total.jsp">계산</a>
</form>
<a href="Exit">로그아웃</a>
</body>
</html>
select
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!--과일의 값을 par로 받는다-->
<%
String fruit = request.getParameter("fruit");
%>
<!--앞에 선언한 arraylist의 set값을 받아옴 -->
<%
session.getAttribute("arr");
/* arrayList또한 list타입으로 형식을 변환해주어야함 */
ArrayList arr = (ArrayList) session.getAttribute("arr");
/* 이제 들어온 value가 선택시 session영역에 1개씩들어온다 */
arr.add(fruit);
/* 이제 add로 들어온 값을 set으로 처리해 다음페이지에 get으로 넘긴다 */
session.setAttribute("arr", arr);
%>
<script>
alert("<%=fruit%>를 장바구니에 담았습니다.");
history.back();
</script>
</head>
<body>
</body>
</html>
add
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="Exit">로그아웃</a>
<h1>장바구니</h1>
<%
ArrayList list = (ArrayList) session.getAttribute("arr");
int cnt[] = {0,0,0,0};
for(int i =0; i<list.size(); i++){
if(list.get(i).equals("사과")){
cnt[0]++;
}else if(list.get(i).equals("배")){
cnt[1]++;
}else if(list.get(i).equals("귤")){
cnt[2]++;
}else if(list.get(i).equals("오렌지")){
cnt[3]++;
}
}
out.print("<table >");
out.print("</tr>");
out.print("<tr>"+"사과 :"+cnt[0]+"</tr>");
out.print("<tr>"+"배 :"+cnt[1]+"</tr>");
out.print("<tr>"+"귤 :"+cnt[2]+"</tr>");
out.print("<tr>"+"오렌지 :"+cnt[3]+"</tr>");
out.print("</tr>");
out.print("</table>");
%>
<%=session.getAttribute("arr")%>
</body>
</html>
total
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%session.invalidate();%>
<%response.sendRedirect("Login.jsp");%>
</body>
</html>
exit
현재 기능구현 1개 소스코드도 비효율적이다 ... 좀더 발전해야겠다..
주말중으로 주석달고 설명추가 + 기능구현