고래씌

[JSP] 피자주문 실습문제 본문

Server/JSP과 Servlet

[JSP] 피자주문 실습문제

고래씌 2023. 12. 6. 12:41

 

 

■ indexhtml 파일

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<h2>피자주문페이지(jsp) --> 주문 요청 --> 주문요청을 받아서 처리해주는 servlet --> 응답페이지</h2>
	
	<a href="views/pizzaOrderForm.jsp">피자주문페이지</a>
	
</body>
</html>

 

 

■ pizzaOrderFrom.jsp 파일

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Date, java.text.SimpleDateFormat"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
	span{
		color: pink;
	}

	.price, .type, th{
		text-align: center;

	}

	table, td, th{
		border : 1px solid black;
		border-collapse : collapse;
	}


</style>
</head>
<body>
	<%
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 E요일");
		String today = sdf.format(date);
	%>
	<h1>오늘은 <span><%= today  %></span></h1>
	
	<h1>피자아카데미</h1>
	
	<table border="1">
		<thead>
			<tr>
				<th>종류</th>
				<th>이름</th>
				<th style="width:50px;">가격</th>
				<th style="width:20px;"></th>
				<th>종류</th>
				<th>이름</th>
				<th style="width:50px;">가격</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td rowspan="5" class="type">피자</td>
				<td>치즈피자</td>
				<td class="price">5000</td>
				<td rowspan="11"></td>
				<td rowspan="11">사이드</td>
				<td>오븐구이통닭</td>
				<td class="price">9000</td>
			</tr>
			<tr>
				<td>콤비네이션피자</td>
				<td class="price">6000</td>
				<td>치킨스틱&윙</td>
				<td class="price">4900</td>
			</tr>
			<tr>
				<td>포테이토피자</td>
				<td class="price">7000</td>
				<td>치즈오븐스파게티</td>
				<td class="price">4000</td>
			</tr>
			<tr>
				<td>고구마피자</td>
				<td class="price">7000</td>
				<td>새우링&웨지감자</td>
				<td class="price">3500</td>
			</tr>
			<tr>
				<td>불고기피자</td>
				<td class="price">8000</td>
				<td>갈릭포테이토</td>
				<td class="price">3000</td>
			</tr>
			<tr>
				<td rowspan="6" class="type">토핑</td>
				<td>고구마무스</td>
				<td class="price">1000</td>
				<td>콜라</td>
				<td class="price">1500</td>
			</tr>
			<tr>
				<td>콘크림무스</td>
				<td class="price">1500</td>
				<td>사이다</td>
				<td class="price">1500</td>
			</tr>
			<tr>
				<td>파인애플토핑</td>
				<td class="price">2000</td>
				<td>갈릭소스</td>
				<td class="price">500</td>
			</tr>
			<tr>
				<td>치즈토핑</td>
				<td class="price">2000</td>
				<td>피클</td>
				<td class="price">300</td>
			</tr>
			<tr>
				<td>치즈크러스트</td>
				<td class="price">2000</td>
				<td>핫소스</td>
				<td class="price">100</td>
			</tr>
			<tr>
				<td>치즈바이트</td>
				<td class="price">3000</td>
				<td>파마산 치즈가루</td>
				<td class="price">100</td>
			</tr>
		</tbody>
	</table>

	<br>
	
	<form action='/jsp/PizzaServlet' method="post">
		피자 :
		<select name="pizza">
			<option value="치즈피자" name="pizza">치즈피자</option>
			<option value="콤비네이션피자" name="pizza">콤비네이션피자</option>
			<option value="포테이토피자" name="pizza">포테이토피자</option>
			<option value="고구마피자" name="pizza">고구마피자</option>
			<option value="불고기피자" name="pizza">불고기피자</option>
		</select>
		<br>
		토핑 :
		<input type="checkbox" name="topping" value="고구마무스"><label>고구마무스</label>
		<input type="checkbox" name="topping" value="콘크림무스"><label>콘크림무스</label>
		<input type="checkbox" name="topping" value="파인애플토핑"><label>파인애플토핑</label>
		<input type="checkbox" name="topping" value="치즈토핑"><label>치즈토핑</label>
		<input type="checkbox" name="topping" value="치즈크러스트"><label>치즈크러스트</label>
		<input type="checkbox" name="topping" value="치즈바이트"><label>치즈바이트</label>
		
		<br>
		사이드 :
		<input type="checkbox" name="side" value="오븐구이통닭"><label>오븐구이통닭</label>
		<input type="checkbox" name="side" value="치킨스틱&윙"><label>치킨스틱&윙</label>
		<input type="checkbox" name="side" value="치즈오븐스파게티"><label>치즈오븐스파게티</label>
		<input type="checkbox" name="side" value="새우링&웨지감자"><label>새우링&웨지감자</label>
		<input type="checkbox" name="side" value="갈릭포테이토"><label>갈릭포테이토</label>
		<input type="checkbox" name="side" value="콜라"><label>콜라</label>
		<input type="checkbox" name="side" value="사이다"><label>사이다</label>
		<input type="checkbox" name="side" value="갈릭소스"><label>갈릭소스</label>
		<input type="checkbox" name="side" value="피클"><label>피클</label>
		<input type="checkbox" name="side" value="핫소스"><label>핫소스</label>
		<input type="checkbox" name="side" value="파마산 치즈가루"><label>파마산 치즈가루</label>
	
	
		<br>
		<input type="submit" onclick="location.href='views/pizzaDetail.jsp'" value="확인">
	
	</form>
</body>
</html>

 

 

■ RequestPizza.java(Servlet)

package com.kh.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestPizza
 */
@WebServlet("/PizzaServlet")
public class RequestPizza extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestPizza() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String pizza = request.getParameter("pizza");
		String[] toppings = request.getParameterValues("topping");
		String[] sides = request.getParameterValues("side");
		
		request.setAttribute("pizza", pizza);
		request.setAttribute("toppings", toppings);
		request.setAttribute("sides", sides);
		
		// 가격계산(보통의 흐름 : DB에 SQL문 실행한 후 메뉴가격의 합산을 반환)
		int price = 0;
		
		switch(pizza) {
		case "치즈피자" : price += 5000; break;
		case "콤비네이션피자" : price += 6000; break;
		case "포테이토피자" : 
		case "고구마피자" : price += 7000; break;
		case "불고기피자" : price += 8000; break;
		}
		
		if(toppings != null) {
			for(String topping : toppings) {
				switch(topping) {
				case "고구마무스" : price += 1000; break;
				case "콘크림무스" : price += 1500; break;
				case "파인애플토핑" :
				case "치즈토핑" : 
				case "치즈크러스트" : price += 2000; break;
				case "치즈바이트" : price += 3000; break;
				}
			}
		}
		
		if(sides != null) {
			for(String side : sides) {
				switch(side) {
				case "오븐구이통닭" : price += 9000; break;
				case "치킨스틱&윙" : price += 4900; break;
				case "치즈오븐스파게티" : price += 4000; break;
				case "새우링&웨지감자" : price += 3500; break;
				case "갈릭포테이토" : price += 3000; break;
				case "콜라" :
				case "사이다" : price += 1500; break;
				case "갈릭소스" : price += 500; break;
				case "피클" : price += 300; break;
				case "핫소스" :
				case "파마산 치즈가루" : price += 100; break;
				}
			}
		}

		request.setAttribute("price", price);
		
		request.getRequestDispatcher("views/pizzaDetail.jsp").forward(request, response);
		
		
	}

}

 

 

 

■ pizzaDetail 파일

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.HashMap"%>
   
<%
   	String pizza = (String) request.getAttribute("pizza");
	String[] toppings = (String[]) request.getAttribute("toppings");
	String[] sides = (String[]) request.getAttribute("sides");
	int price = (int) request.getAttribute("price");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
	body{
		font-weight: bold;
	}
	
	#pizza{
		color : red;
	}
	
	#topping{
		color : green;
	}
	
	#side{
		color : blue;
	}
	
	h1{
		color : pink;
	}
	
</style>
</head>
<body>
	<h3>주문 내역</h1>
	피자는 <span id="pizza"><%= pizza %></span>,
	토핑은 <span id="topping">
	<% if(toppings == null) {
			out.print("없습니다.");
		}else {
			for(String topping : toppings){
				out.print(topping+ ", ");
			}
		}%>
	
	</span>

	<% if(sides != null) { %>
		사이드는
		<span id="side">
		<%= String.join(", ", sides) %>
			
		<% } %>
	</span> 
	주문하셨습니다.
	
	<br><br><br><br>

	총합 : <%= price %> 원
	
	<br><br>
	
	<h1>즐거운 식사시간 되세요 ~</h1>
	

</body>
</html>