JSP/액션 태그
자바빈즈 액션 태그
별초롱언니
2025. 5. 22. 14:07
JSP 페이지의 주요 기능 중 하나는 데이터를 보여주는 것입니다. 그런데 하나의 JSP 페이지에서 데이터를 보여주기 위한 자바 코드와 단순히 화면을 출력하는 HTML 코드를 함께 작성하면 기능을 확장하거나 코드를 재사용하는 데 어려움이 있습니다. 따라서 프로그램의 효율을 높이기 위해 화면을 출력하는 부분과 데이터를 처리하는 로직 부분을 구분하여 작성하고, 로직부분의 코드에는 자바빈즈라는 클래스를 사용합니다.
자바빈즈는 동적 콘텐츠 개발을 위해 자바 코드를 사용하여 자바 클래스로 로직을 작성하는 방법입니다. 다시 말해 JSP 페이지에서 화면을 표현하기 위한 계산식이나 자료의 처리를 담당하는 자바 코드를 따로 분리하여 작성하는 것을 의미합니다.
자바빈즈는 데이터 표현을 목적으로 하는 자바 클래스이기 때문에 기존의 자바 클래스를 작성하는 방법과 동일합니다. 자바빈즈는 데이터를 담는 멤버 변수인 프로퍼티와 데이터를 가져오거나 저장하는 메소드로 구성됩니다. 자바빈즈를 작성할 때는 다음 규칙을 따라야합니다.
- 자바 클래스는 java.jo.Serializable 인터페이스를 구현해야 합니다.
- 인수가 없는 기본 생성자가 있어야합니다.
- 모든 멤버 변수인 프로퍼티는 private 접근 지정자로 설정해야합니다.
- 모든 멤버 변수인 프로퍼티는 Getter/Setter() 메소드가 존재해야 합니다. Getter() 메소드는 멤버 변수에 저장된 값을 가져올 수 있는 메소드이고, Setter() 메소드는 멤버 변수에 값을 저장 할 수 있는 메소드입니다.
이렇게 작성된 자바빈즈는 JSP 페이지에서 usbBean, setProperty, getProperty 등의 자바빈즈 액션 태그와 스크립트 태그에 자바 코드와 같이 사용할 수 있습니다. 또한 폼 페이지의 입력 데이터나 HTML 페이지에서 넘어오는 데이터를 쉽게 자바빈즈 객체로 저장할 수 있습니다.
<%@ 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="date" class="java.util.Date" />
<p> <%
out.print("오늘의 날짜 및 시각");
%>
<p> <%=date %>
</body>
</html>
<%@ 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="bean" class="ch04.com.dao.Calculator" />
<%
int m = bean.process(5);
out.print("5의 3제곱 : " + m);
%>
</body>
</html>
package ch04.com.dao;
public class Calculator {
public int process (int n) {
return n * n * n;
}
}
package chapter04;
public class Person {
private int id = 20230821;
private String name = "홍길순";
public Person() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<%@ 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="person" class="chapter04.Person" scope="request" />
<p> 아이디 : <%=person.getId() %>
<p> 이 름 : <%=person.getName() %>
</body>
</html>
<%@ 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="person" class="chapter04.Person" scope="request" />
<p> 아이디 : <%=person.getId() %>
<p> 이 름 : <%=person.getName() %>
<%
person.setId(20230824);
person.setName("홍길동");
%>
<jsp:include page = "useBean03.jsp" />
</body>
</html>
<%@ 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="person" class="chapter04.Person" scope="request" />
<jsp:setProperty name="person" property="id" value="20230824" />
<jsp:setProperty name="person" property="name" value="홍길동" />
<p> 아이디 : <%=person.getId() %>
<p> 이 름 : <%=person.getName() %>
</body>
</html>
<%@ 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="person" class="chapter04.Person" scope="request" />
<p> 아이디 : <jsp:getProperty name="person" property="id" />
<p> 이 름 : <jsp:getProperty name="person" property="name" />
</body>
</html>
<%@ 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="person" class="chapter04.Person" scope="request" />
<jsp:setProperty name="person" property="id" value="20230824" />
<jsp:setProperty name="person" property="name" value="홍길동" />
<p> 아이디 : <jsp:getProperty name="person" property="id" />
<p> 이 름 : <jsp:getProperty name="person" property="name" />
</body>
</html>