아이디를 입력받아 로그인 하고 상품 리스트 중 하나를 추가하여 장바구니에 넣고
계산 누르면 전체 장바구니 내역을 확인 할 수 있는 페이지를 만드세요
(JSP와 SESSION의 활용 입니다.)
[login.jsp]
<%@ 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 charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%session.invalidate(); %>
<center>
<h2>로그인</h2><hr>
<form action="setProduct.jsp" method="get">
<input type="text" size=20 name="username">
<input type="submit" value="로그인">
</form>
</center>
</body>
</html>
[setProduct.jsp]
<%@ 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 charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("euc-kr");%>
<%
session.setAttribute("username",request.getParameter("username"));
%>
<center>
<h2>#<%= session.getAttribute("username") %>님 환영합니다.<br></h2><hr>
<form action="add.jsp">
<select name = "fruit">
<option>사과</option>
<option>바나나</option>
<option>딸기</option>
<option>자몽</option>
<option>오렌지</option>
</select>
<input type="submit" value="장바구니 추가">
<input type="button" value="계산" onclick="location.href='checkout.jsp';">
<input type="button" value="로그아웃" onclick = "logout();">
</form>
<script language="javascript">
function logout(){
alert("세션이 종료되었습니다.");
document.location.href="login.jsp";
}
</script>
</center>
</body>
</html>
[add.jsp]
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<%
if(session.getAttribute("arr") == null){
ArrayList<String> arr = new ArrayList<String>();
arr.add(request.getParameter("fruit"));
session.setAttribute("arr", arr);
}
else {
ArrayList<String> arr2 = (ArrayList) session.getAttribute("arr");
arr2.add(request.getParameter("fruit"));
session.setAttribute("arr", arr2);
}
%>
</head>
<body>
<script>
alert("<%=request.getParameter("fruit") %>"+"가 추가되었습니다."); history.back();
</script>
</body>
</html>
[checkout.jsp]
<%@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 charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h2><%= session.getAttribute("username") %>님 환영합니다.<br></h2><hr>
<%
ArrayList<String> arr= (ArrayList) session.getAttribute("arr");
try{
for(int i =0; i<arr.size();i++)
out.println(arr.get(i)+"<br>");
}catch(NullPointerException e){
out.print("장바구니가 비었습니다.");
}
%>
</center>
</body>
</html>
'Servlet & JSP' 카테고리의 다른 글
beans 예제 (0) | 2019.06.25 |
---|---|
REQUEST 예제_SCOPE 안쓰고 데이터 넘기기 (0) | 2019.06.24 |
JSP 예제_setAttribute (0) | 2019.06.21 |
JSP 예제_forward/sendRedirect (0) | 2019.06.21 |
JSP 예제_쿠키 (0) | 2019.06.21 |