본문 바로가기
[2020]KIC 캠퍼스 복습/JAVA SCRIPT(5,6,7)

[코드리뷰] Servlet) Form Beans 예제 1

by 두블두블 2020. 8. 26.

문제 1 +  강사님

Servlet의 Form Beans에 대해 알아보자 

 


학습목표

1. Form benas을 활용할 수 있다. 

2. 비즈니스 클래스를 활용하자 


<코드 1 : buyingmain.html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>문제풀이 : 입력창</title>
</head>
<body><form action = "result.jsp" method="post">
과일명 : <input type="text" name = "name" value="사과"><br>
정가 : <input type="text" name = "price" value="5000"><br>
할인 : <input type="text" name = "dis" value="1000"><br>
<input type="submit" value="전송"><br>
</body>
</html>

 

<코드 2 : result.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="fruit" class="pack.NameList" />
<jsp:setProperty property="*" name="fruit"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>결과창</title>
</head>
<body>
계산 결과<br>
과일 "<jsp:getProperty property="name" name="fruit"/>"의 :

<jsp:useBean id="bc" class="pack.BusinessClass" />
<jsp:setProperty property="cal" name="bc" value="<%=fruit %>"/>
소비자 실 구매가격은 <jsp:getProperty property="tot" name = "bc" /></body>
</html>

 

<코드 3 : NameList.java>

package pack;

public class NameList {
	private String name;
	private int price, dis;
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getDis() {
		return dis;
	}
	public void setDis(int dis) {
		this.dis = dis;
	}
}

<코드 4 : BusinessClass.java>

package pack;

public class BusinessClass {
	private NameList cal;
	
	public void setCal(NameList cal) {
		this.cal = cal;
	}
	
	public int getTot() {
		return cal.getPrice() - cal.getDis();
	}	
}

 


결과화면

 

 

<입력창>


<결과창>