CLASS/Web Server
[웹서버프로그래밍] 2022.04.05 Assign 04
sseni
2022. 4. 11. 12:18
728x90
교재 234페이지를 수정하여 각 과일의 주문 개수와 금액 및 총 주문 개수와 총 주문 금액을 출력하는 프로그램을 작성하여 pdf 파일로 제출.
- login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ch06 : login.jsp</title>
</head>
<body>
<div align="center">
<H2>로그인</H2>
<form name="form1" method="POST" action="selProduct.jsp">
<input type="text" name="username"/>
<input type="submit" value="로그인"/>
</form>
</div>
</body>
</html>
- selProduct.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>ch06 : selProduct.jsp</title>
</head>
<%
request.setCharacterEncoding("UTF-8"); // euc-kr
session.setAttribute("username",request.getParameter("username"));
%>
<body>
<div align="center">
<H2>상품선택</H2>
<HR>
<%=session.getAttribute("username") %>님 환영합니다!!!!
<HR>
<form name="form1" method="POST" action="add.jsp">
<SELECT name="product">
<option>사과</option>
<option>귤</option>
<option>파인애플</option>
<option>자몽</option>
<option>레몬</option>
</SELECT>
<input type="submit" value="추가"/>
</form>
<a href="checkOut.jsp">계산</a>
</div>
</body>
</html>
- add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ch05 : add.jsp</title>
</HEAD>
<body>
<%
request.setCharacterEncoding("UTF-8");
String productname = request.getParameter("product");
ArrayList<String> list = (ArrayList)session.getAttribute("productlist");
if(list == null) {
list = new ArrayList<String>();
session.setAttribute("productlist",list);
}
list.add(productname);
%>
<script>
alert("<%=productname %> 이(가)추가 되었습니다.!!");
history.go(-1);
</script>
</body>
</html>
- checkOut.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>ch06 : checkOut.jsp</title>
</HEAD>
<body>
<div align="center">
<H2>계산</H2>
선택한 상품 목록
<HR>
<%
ArrayList list = (ArrayList)session.getAttribute("productlist");
ArrayList cnt = new ArrayList();
int sum = 0;
if(list == null) {
out.println("선택한 상품이 없습니다.");
}
else {
for(Object productname:list) {
if(!cnt.contains(productname)){
cnt.add(productname);
out.println(productname + " " + Collections.frequency(list, productname) + "개<BR>");
}
if(productname.equals("사과")) sum += 100;
if(productname.equals("귤")) sum += 200;
if(productname.equals("파인애플")) sum += 300;
if(productname.equals("자몽")) sum += 400;
if(productname.equals("레몬")) sum += 500;
}
out.print("<BR>총 금액은 " + sum + "원입니다.<BR>");
}
%>
</div>
</body>
</html>
728x90