자바빈은 JSP 페이지의 디자인 부분과 비즈니스 로직 부분을 분리함으로써 복잡한 JSP 코드들을 줄이고, 프로그램의 재사용성을 증가시키고 개발 시 분리된 작업을 가능하게 한다.
BeanTest.java
package test;
public class BeanTest {
private String name = "honggildong";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
beanTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class="test.BeanTest" scope="page" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaBean Test</title>
</head>
<body>
<b>자바빈 사용 예제</b>
<h3><%=beantest.getName() %></h3>
</body>
</html>
실행 화면
<jsp:useBean id="빈 이름" class="자바빈 클래스 명" "scope="사용범위"/>
<jsp:setProperty name="빈 이름" property="속성명" value="설정할 속성 값"/>
사용 예제
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class="test.BeanTest" scope="page" />
<jsp:setProperty name = "beantest" property="name" value="BeanTest!"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaBean Test</title>
</head>
<body>
<b>자바빈 사용 예제</b>
<h3><%=beantest.getName() %></h3>
</body>
</html>
beanTest.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="beanTest3.jsp">
이름:<input type = "text" name = "name" />
<br/>
<input type = "submit" value = "전송"/>
</form>
</body>
</html>
beanTest3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class="test.BeanTest" scope="page"></jsp:useBean>
<jsp:setProperty name="beantest" property="name" param="name"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaBean Test</title>
</head>
<body>
<b>자바빈 사용 예제</b>
<h3><%=beantest.getName() %></h3>
</body>
</html>
beanTest3.jsp 페이지로 name 속성 값을 전송한다.
클라이언트에서 전송되어 오는 파라미터 이름이 빈객체의 속성명과 모두 같다면 다음과 같이 한 번에 할당할 속성명을 할당할 수 있다.
<jsp:setProperty name="빈 이름" property="*"/>
BeanTest2.java
package test;
public class BeanTest2 {
private String name;
private String addr;
private String email;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
beanTest2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<section id = "formArea">
<h1>property = "*" 테스트</h1>
<form action = "beanTest4.jsp" method = "post">
<fieldset>
<label for = "name">이름 :</label><input type = "text" name = "name" id = "name"/><br>
<label for = "addr">주소 :</label><input type = "text" name = "addr" id = "addr"/><br>
<label for = "email">이메일 주소 :</label><input type = "email" name = "email" id = "email" /><br>
<label for = "birthday">생년 월일 :</label><input type = "date" name = "birthday" id = "birthday"/><br>
<input type = "submit" value = "전송">
</fieldset>
</form>
</section>
</body>
</html>
전송 화면
출력 화면
<jsp:getProperty name="빈 이름" property="속성명"/>
BeanTest5.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="beantest" class="test.BeanTest" scope="page" />
<jsp:setProperty name = "beantest" property = "name" value="지지고"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaBean Test</title>
</head>
<body>
<b>자바빈 사용 예제</b>
<h3><%=beantest.getName() %></h3>
<h3><jsp:getProperty name="beantest" property="name" /></h3>
</body>
</html>