JavaBean
java파일로(객체)를 생성하고, jsp 필요에 따라 class를 가져다 쓰는데 이 때 class(객체)를 빈 이라고 부른다.
반복적인 코드를 따로 작성할 수 있께 하여 코드를 재사용하기 위한 기술이다.
자바빈즈 액션태그
빈 관련 태그 | 설명 |
<jsp:useBean id="..." class="..." scope="..." /> | 빈을 생성하여 둔다. |
<jsp:setProperty name="..." property="..." value="..." /> | 빈에 값을 저장한다. |
<jsp:getProperty name="..." property="..." /> | 빈의 값을 가져온다. |
Scope 종류
종류 | 설명 |
page | 해당 jsp페이지 내에서만 존재 |
request | 사용자의 요청을 처리하는 동안에만 존재 |
session | 사용자가 최초에 접속하여 접속이 종료되기 전 까지 존재 |
application | 해당 어플리케이션이 살아있는 동안 함께 존재 |
빈 생성
class login {
private String id; // property
private String passwd; // property
public String getId() { // getter
return id;
}
public String setId(String id) { // setter
this.id =id;
}
public String getPasswd() { // getter
return passwd;
}
public String setPasswd(String passwd) { // setter
this.passwd = passwd;
}
}
getter 메서드는 값을 읽어오는 역할, setter 메서드는 프로퍼티의 값을 설정하는 역할
자바빈은 데이터베이스와 연동할 때 자주 사용되고,
이렇게 구성된 파일은 DTO(Date Transfer Object) 또는 VO(Valiue Object)라는 데이터 교환을 위한 객체로 사용된다.
// useBean
<jsp:userBean id="이름" scope="page | request | session | application"
class="클래스명" type="타입명" />
// getProperty
<jsp:getProperty name="빈이름" property="필드명" />
// setProperty
<jsp:setProperty name="빈이름" property="필드명" value="값 />
<jsp:setProperty name="빈이름" property="*" />
useBean 액션 태그는 특정한 자바빈 파일을 사용 한다고 명시할 때 사용된다.
class 속성명은 클래스를 의미하며 import가 포함되어 있다.
scope속성은 유효범위를 의미하며 page(생성된 페이지 내), request(요청된 페이지 내), session(웹 브라우저의 생명 주기)
, application (어플리케이션의 생명주기) 을 작성할 수 있으며, 기본 값은 page이다.
setProperty 액션 태그는 자바 빈 파일의 setter 메서드를 사용하기 위해, 즉 데이터의 값을 설정할 때 사용한다.
property 속성에 * 을 사용하면 프로퍼티와 동일한 이름의 파라미터를 이용해 setter 메서드를 생성한 모든 프로퍼티에 대해 값을 설정할 수 있다.
getProperty 액션 태그는 자바 빈 파일의 getter 메서드를 사용하기 위해, 즉 저장된 데이터의 값을 읽어올 때 사용된다.
예시
// SimpleBean.java
package Ch05;
public class SimpleBean {
private String msg1;
private String msg2;
public String getMsg1() {
return msg1;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
public String getMsg2() {
return msg2;
}
public void setMsg2(String msg2) {
this.msg2 = msg2;
}
}
// SimpleBean.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 빈 객체 생성 -->
<jsp:useBean id="test1" class="Ch05.SimpleBean" scope="page" />
<!-- 빈 객체 속성값 저장 -->
<jsp:setProperty name="test1" property="msg1" value="MSG1 값" />
<jsp:setProperty name="test1" property="msg2" value="MSG2 값" />
<!-- 빈 객체 속성값 확인 -->
MSG1 : <jsp:getProperty name="test1" property="msg1" /> <br>
MSG1 : <jsp:getProperty name="test1" property="msg2" /> <br>
<hr>
<%@page import="Ch05.SimpleBean" %>
<%
SimpleBean bean = new SimpleBean();
bean.setMsg1("message1");
bean.setMsg2("message2");
%>
MSG1 = <%=bean.getMsg1() %><br>
MSG2 = <%=bean.getMsg2() %><br>
</body>
</html>
'Backend > JSP SERVLET' 카테고리의 다른 글
[JSP] 세션(Session) (0) | 2022.06.28 |
---|---|
[JSP] 내장 객체(implicit object) (0) | 2022.06.28 |
[JSP] 포워딩, 리다이렉트 (0) | 2022.06.27 |
[JSP] form (0) | 2022.06.26 |
[JSP] ActionTag (0) | 2022.06.24 |