본문 바로가기
IT

[쉽게 배우는 JSP 웹프로그래밍] 14장 연습문제 및 솔루션 총정리

by nutrient 2021. 10. 23.
728x90
728x170

1. JSP 페이지에 쿠키를 설정하는 메소드, 설정된 쿠키 정보를 얻어오는 메소드는 무엇인가?

쿠키 설정은 Cookie Cookie(String name, String value)로 쿠키를 생성한 후에, response 내장 객체의 addCookie() 메소드로 쿠키를 설정한다. 또한 설정된 쿠키 정보를 얻어오는 메소드는 getCookie()를 사용한다.


2. 설정된 쿠키를 삭제하는 기법은 무엇인가?

setMaxAge() 메소드를 통해 쿠키의 유효 기간을 0으로 설정하면 쿠키를 삭제할 수 있다.


3. 쿠키를 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>

<form action="cookie_process.jsp" method="post">
<p> 아이디 : <input type="text" name="id"> 
<p> 비밀번호 : <input type="text" name="passwd">
<input type="submit" value="전송">

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>

<%
String id = request.getParameter("id");
String pw = request.getParameter("passwd");

if(id.equals("admin") && pw.equals("admin1234")){
	Cookie cookieID = new Cookie("userID", id);
	response.addCookie(cookieID);
	response.sendRedirect("welcome.jsp");
}else{
	out.println("세션 연결에 실패했습니다.");
}
%>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>

<%
Cookie[] cookies = request.getCookies();
cookies[0].setMaxAge(0);
response.addCookie(cookies[0]);	

response.sendRedirect("cookie.jsp");
%>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>

<%
Cookie[] userid = request.getCookies();

if(userid[0] == null){
	response.sendRedirect("cookie_out.jsp");
}
%>

<h3><%= userid[0].getName() %>님 반갑습니다.</h3>
<a href="cookie_out.jsp">로그아웃</a>

</body>
</html>

 

4. 다음 조건에 맞게 도서 웹 쇼핑몰을 위한 웹 애플케이션을 만들고 실행 결과를 확인하시오.

<td align="right"><a href="./shippingInfo.jsp?cartId=<%=cartId %>" class="btn btn-success">주문하기</a></td>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>배송 정보</title>
</head>
<body>

<%@ include file="menu.jsp" %>

<div class="jumbotron">
	<div class="container">
		<h1 class = "display-3">배송 정보</h1>
	</div>
</div>

<div class="container">
	<form action="./processShippingInfo.jsp" class="form-horizontal" method="post">
		<input type="hidden" name="cartId" value="<%= request.getParameter("cartId") %>"/>
		<div class="form-group row">
			<label class="col-sm-2">성명</label>
			<div class="col-sm-3">
				<input name="name" type="text" class="form-control"/>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">배송일</label>
			<div class="col-sm-3">
				<input name="shippingDate" type="text" class="form-control"/>(yyyy/mm/dd)
			</div>
		</div>
			<div class="form-group row">
			<label class="col-sm-2">국가명</label>
			<div class="col-sm-3">
				<input name="country" type="text" class="form-control"/>
			</div>
		</div>
					<div class="form-group row">
			<label class="col-sm-2">우편번호</label>
			<div class="col-sm-3">
				<input name="zipCode" type="text" class="form-control"/>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2">주소</label>
			<div class="col-sm-5">
				<input name="addressName" type="text" class="form-control"/>
			</div>
		</div>
		<div class="form-group row">
			<div class="col-sm-offset-2 col-sm-10">
				<a href="./cart.jsp?cartId=<%=request.getParameter("cartId")%>" class="btn btn-secondary" role="button"> 이전</a>
				<input type="submit" class="btn btn-primary" value="등록"/>
				<a href="./checkOutCancelled.jsp" class="btn btn-secondary" role="button">취소</a>
			</div>
		</div>		
	</form>
</div>

</body>
</html>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="dto.Product" %>
<%@ page import="dao.ProductRepository" %>

<%
request.setCharacterEncoding("UTF-8");

String cartId = session.getId();
String shipping_cartId = "";
String shipping_name = "";
String shipping_shippingDate = "";
String shipping_country = "";
String shipping_zipCode = "";
String shipping_addressName = "";

Cookie[] cookies = request.getCookies();

if(cookies != null){
	for(int i=0; i<cookies.length; i++){
		Cookie thisCookie = cookies[i];
		String n = thisCookie.getName();
		if(n.equals("Shipping_cartId"))
			shipping_cartId = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_name"))
			shipping_name = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_shippingDate"))
			shipping_shippingDate = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_country"))
			shipping_country = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_zipCode"))
			shipping_zipCode = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_addressName"))
			shipping_addressName = URLDecoder.decode((thisCookie.getValue()), "utf-8");
	}
}
%>

<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>주문 정보</title>
</head>
<body>

<%@ include file="menu.jsp" %>

<div class="jumbotron">
	<div class="container">
		<h1 class = "display-3">주문정보</h1>
	</div>
</div>

<div class="container col-8 alert alert-info">
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">주문 정보</h1>
		</div>
	</div>

	<div class="row justify-content-between">
		<div class="col-4" align="left">
			<strong>배송주소</strong>
			<br> 성명 : <% out.println(shipping_name); %>
			<br> 우편번호 : <% out.println(shipping_zipCode); %>
			<br> 주소 : <% out.println(shipping_addressName); %><% out.println(shipping_country); %>
			<br>			
		</div>
		<div class="col-4" align="right">
			<p> <em>배송일 : <% out.println(shipping_shippingDate); %></em>
		</div>
	</div>
	
	<div>
		<table class="table table-hover">
			<tr>
				<th>도서</th>
				<th>#</th>
				<th>가격</th>
				<th>소계</th>			
			</tr>
			<%
			int sum = 0;
			ArrayList<Product> cartList = (ArrayList<Product>)session.getAttribute("carlist");
			if(cartList == null)
				cartList = new ArrayList<Product>();
			for(int i=0; i<cartList.size(); i++){
				Product product = cartList.get(i);
				int total = product.getUnitPrice() * product.getQuantity();
				sum += total;
			%>
			<tr>
				<td class="text-center"><em><%=product.getPname()%></em></td>
				<td class="text-center"><%=product.getQuantity() %></td>
				<td class="text-center"><%=product.getUnitPrice() %></td>
				<td class="text-center"><%=total%>원</td>
			</tr>
			<%
			}
			%>
			<tr>
				<td></td>
				<td></td>
				<td class="text-right"><strong>총액 : </strong></th>
				<td class="text-center text-danger"><strong><%=sum %> </strong></th>
			</tr>
		</table>
		
		<a href="./shippingInfo.jsp?cartId=<%=shipping_cartId%>" class="btn btn-secondary" role="button">이전</a>
		<a href="./thankCustomer.jsp" class="btn btn-success" role="button">주문 완료</a>
		<a href="./checkOutCancelled.jsp" class="btn btn-secondary" role="button">취소</a>
	</div>
</div>
</body>
</html>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.net.URLDecoder" %>

<%
request.setCharacterEncoding("UTF-8");

String cartId = session.getId();
String shipping_cartId = "";
String shipping_name = "";
String shipping_shippingDate = "";
String shipping_country = "";
String shipping_zipCode = "";
String shipping_addressName = "";

Cookie[] cookies = request.getCookies();

if(cookies != null){
	for(int i=0; i<cookies.length; i++){
		Cookie thisCookie = cookies[i];
		String n = thisCookie.getName();
		if(n.equals("Shipping_cartId"))
			shipping_cartId = URLDecoder.decode((thisCookie.getValue()), "utf-8");
		if(n.equals("Shipping_shippingDate"))
			shipping_shippingDate = URLDecoder.decode((thisCookie.getValue()), "utf-8");
	}
}


%>

<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>주문 완료</title>
</head>
<body>

<%@ include file="menu.jsp" %>

<div class="jumbotron">
	<div class="container">
		<h1 class = "display-3">주문 완료</h1>
	</div>
</div>

<div class="container">
	<h2 class="alert alert-danger">주문해주셔서 감사합니다.</h2>
	<p> 주문은 <% out.println(shipping_shippingDate); %>에 배송될 예정입니다!
	<p> 주문 번호 : <% out.println(shipping_cartId); %>
</div>

<div class="container">
	<p><a href="./products.jsp" class="btn btn-secondary">&laquo; 상품 목록</a>
</div>

</body>
</html>

<%
session.invalidate();

for(int i=0; i<cookies.length; i++){
	Cookie thisCookie = cookies[i];
	String n = thisCookie.getName();
	if(n.equals("Shipping_cartId"))
		thisCookie.setMaxAge(0);
	if(n.equals("Shipping_name"))
		thisCookie.setMaxAge(0);
	if(n.equals("Shipping_shippingDate"))
		thisCookie.setMaxAge(0);
	if(n.equals("Shipping_country"))
		thisCookie.setMaxAge(0);
	if(n.equals("Shipping_zipCode"))
		thisCookie.setMaxAge(0);
	if(n.equals("Shipping_addressName"))
		thisCookie.setMaxAge(0);
}
%>
<%@page contentType="text/html; charset=utf-8"%>
<%@page import ="java.net.URLEncoder" %>

<%
request.setCharacterEncoding("UTF-8");

Cookie cartId = new Cookie("Shipping_cartId", URLEncoder.encode(request.getParameter("cartId"), "utf-8"));
Cookie name = new Cookie("Shipping_name", URLEncoder.encode(request.getParameter("name"), "utf-8"));
Cookie shippingDate = new Cookie("Shipping_shippingDate", URLEncoder.encode(request.getParameter("shippingDate"), "utf-8"));
Cookie country = new Cookie("Shipping_country", URLEncoder.encode(request.getParameter("country"), "utf-8"));
Cookie zipCode = new Cookie("Shipping_zipCode", URLEncoder.encode(request.getParameter("zipCode"), "utf-8"));
Cookie addressName = new Cookie("Shipping_addressName", URLEncoder.encode(request.getParameter("addressName"), "utf-8"));

cartId.setMaxAge(24*60*60);
name.setMaxAge(24*60*60);
zipCode.setMaxAge(24*60*60);
country.setMaxAge(24*60*60);
addressName.setMaxAge(24*60*60);

response.addCookie(cartId);
response.addCookie(name);
response.addCookie(shippingDate);
response.addCookie(country);
response.addCookie(zipCode);
response.addCookie(addressName);

response.sendRedirect("orderConfirmation.jsp");

%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- 합쳐지고 최소화된 최신 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>주문 취소</title>
</head>
<body>

<%@ include file="menu.jsp" %>

<div class="jumbotron">
	<div class="container">
		<h1 class="display-3">주문 취소</h1>
	</div>
</div>
<div class="container">
	<h2 class="alert alert-danger">주문이 취소되었습니다.</h2>
</div>
<div>
	<p><a href="./products.jsp" class="btn btn-secondary"> &laquo; 상품 목록</a>
</div>

</body>
</html>

 

[쉽게 배우는 JSP 웹프로그래밍] 13장 연습문제 및 솔루션 총정리

 

[쉽게 배우는 JSP 웹프로그래밍] 13장 연습문제 및 솔루션 총정리

1. 세션이란 무엇인가? 세션은 클라이언트와 웹 서버 간의 상태를 지속적으로 유지하는 방법이다. 세션은 웹 서버에서만 접근이 가능하기 때문에 보안에 유리하며, 브라우저마다 하나씩 존재하

tistorysolution.tistory.com

 

728x90
그리드형

댓글