본문 바로가기
IT

[ 웹 프로그래밍 / jsp / eclipse ] session 장바구니 기능 구현

by 배애앰이 좋아 2020. 4. 27.
반응형

eclipse에서 장바구니 기능을 구현하였습니다. 밑에는 구현 화면과 그에 따른 코드가 있습니다.

처음에 login.jsp에서 실행시키게 되면 아래 화면이 나옵니다.

 

로그인 화면

 

여기서 이름을 친 후 로그인을 하면 다음 화면으로는

 

상품 선택 화면

 

위 화면이 나오고 목록으로 저희가 추가 목록 중 한 가지를 골라서 추가할 수 있습니다.

 

추가 버튼 누를 시 화면4

 

제대로 추가되었는지 확인 화면

 

목록 보기 누를 시 화면 / 최종 화면

 

최종적으로 추가한 모든 목록들을 목록 화면에서 쭉 보여줍니다.

 

- Product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
	body{text-align:center;}
</style>
</head>
<%
	request.setCharacterEncoding("UTF-8");
	String name = request.getParameter("name");
	session.setAttribute("name", name);
%>
<body>
	<H2>상품 선택</H2>
	<hr>
	<h2><%= name %>님 환영합니다!</h2>
	<form name="f2" method="post" action="add.jsp">
		<select name="product">
			<option>감</option>
			<option>사과</option>
			<option>귤</option>
			<option>배</option>
			<option>포도</option>
		</select>
		<input type = "submit" value="추가"/>
	</form>
	<hr>
	<a href="checkput.jsp">목록 보기</a>
</body>
</html>​

 

- Checkput.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style> body{text-align:center;} </style>
<%@ page import = "java.util.*" %>
<body>
	<H2>계산</H2>
	<H2>선택한 상품 목록</H2>
	<hr>
	<%
		ArrayList list = (ArrayList)session.getAttribute("productlist");
		if(list == null) 
			out.println("선택한 상품이 없습니다.");
		else{
			for(Object product:list)
				out.println(product + "<br>");
		}
	%>
</body>
</html>​

 

- Add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ page import = "java.util.*" %>
<%
	request.setCharacterEncoding("UTF-8");
	String product = request.getParameter("product");
	ArrayList list = (ArrayList)session.getAttribute("productlist");
	if(list == null) 
		list = new ArrayList();
	list.add(product);
	session.setAttribute("productlist", list);
	out.println(product+"이(가) 추가되었습니다.");
%>
</body>
</html>

 

- Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<% 	session.invalidate(); %>
<style>
	body{text-align:center;}
</style>
<body>
	<h2>로그인</h2>
        <form name ="f1" method="post" action="product.jsp">
           <input type="text" name="name" >
           <input type="submit" value="로그인" >
        </form>
</body>
</html>

 

참고 사이트 : https://m.blog.naver.com/jsky10503/221129836110

 

JSP - 내장객체 session을 이용한 장바구니

쇼핑몰 사이트에서의 장바구니 기능을 살펴보도록 한다. 1. 사용자가 로그인한다2. 원하는 만큼 상...

blog.naver.com

 

반응형

댓글