표준 액션태그와 JSP 자바빈즈
JSP 내장 객체는 컨테이너가 자바 소스로 변환 할때 미리 인스턴화 되고 초기화되어서
JSP페이지 내에서 바로 사용할 수 있다.
따라서 서블릿 코드를 작성 할때보다 훨씬 간결하게 API를 이용하여 JSP페이지를 작성 할 수있다.
XML기반 JSP 태그도 내장객체와 동일한 장점이있다.
XML 기반 JSP 태그는 JSP페이지 작성 시 자주 사용하는 기능을 미리 구현해 놓고 XML문법 형태로 실행 할 수 있다.
JSP 에서 XML 기반 태그는 두가지 있다.
1. 많이 쓰이는 기능을 JSP 스펙으로 이미 정의 한 다음 컨테이너마다 동일하게 구현 해놓은 '표준 액션 태그' 이다.
표준액션 태그는 액션 태그명과 속성 그리고 하위 요소들이 정해져 있고 수행되는 기능도 정해져있다.
특별한 선언 없이 jsp라는 접두어를 붙여 태그명만 명시하면 컨테이너가 인식해서 수행한다.
2. 커스텀 태그 개발자가 개발해서 사용하는태그 (거의 안쓰임 )
표준액션태그
표준 액션 태그는 미리 정해진 기능들을 JSP스펙에 명시함으로써 모든 컨테이너가 동이하게 구현하는 태그이다.
모든 JSP 컨테이너에서 기본으로 제공하고 있어서 기본 액션 태그라고도 한다.
표준 액션 태그 종류
- jsp:output
- jsp:param
- jsp:params
- jsp:plugin
- jsp:setProperty
- jsp:useBean
<jsp:forward>
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%@ 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> <h3>example14</h3> <jsp:forward page="test.jsp"/> </body> </html> | cs |
웹브라우저가 "example14"를 출력한다. 이부분은 출력되지 않난다.
같은 디렉터리에 있는 test.jsp 페이지로 이동한후 test.jsp 페이지의 실행 결과가 클라이언트로 응답된다.
그래서 example.jsp 에서 출력한 내용으 클라이언트가 받지 못한다.
<jsp:include>
지시자로 포함하면 JSP 파일에서 자바 소스로 변환 될때 B 페이지가 포함된다. 소스가 포함된후 컴파일 되어
클래스파일이 만들어 진다. A.jsp 파일에
<jsp:include page="B"> 방식으로 파일을 포함하면 JSP파일에서 자바소스로 변환 될때 포함되는 지시자와 달리
실행 될때 포함됨
따라서 동적으로 포함 하려면 incldue 표준 액션 태그를 사용한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ 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> <h3>==include 전 </h3> <jsp:include page="test.jsp"></jsp:include> <h3>==include 후 </h3> </body> </html> | cs |
JSP 자바빈즈
JSP 자바빈즈 개발 규약
(1) 패키지화
(2) 기본 생성자 존재
(3) 멤버 변수 접근자는 private로 선언
(4) gettter 메소드
(5) setter 메소드
(6) getter와 setter 접근자 public 선언
(7)직렬화 구현 (선택사항)
객체 직렬화
빈즈생성하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.edu.beans; public class HelloBeans { private String name; private String number; public HelloBeans() { this.name = "이름이 없습니다"; this.number = "번호가 없습니다"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } | cs |
JSP 자바빈즈 사용
멤버변수이름을 property
참조변수 명을 name
값을 value
<jsp:useBean>
id 속성
class 속성
type 속성
scope 속성
<jsp:getProperty>
name 속성
property 속성
<jsp:setProperty>
name 속성
property 속성
value 속성
param 속성
Property ="*"
활용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ 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 class="com.edu.beans.HelloBeans" id="hello"/> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name="hello"/><br> <jsp:setProperty property="name" name="hello" value="Amy"/> <jsp:setProperty property="number" name="hello" value="12345"/> <hr> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name= "hello"/><br> </body> </html> | cs |
번호가 없습니다
Amy
12345
기존파일에 setProperty 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ 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 class="com.edu.beans.HelloBeans" id="hello"/> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name="hello"/><br> <jsp:setProperty property="name" name="hello" param="a"/> <jsp:setProperty property="number" name="hello" param="b"/> <hr> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name= "hello"/><br> </body> </html> | cs |
http://localhost:8081/edu/example17.jsp?a=John&b=6969
브라우저에서 a와 b를 전달
이름이 없습니다
번호가 없습니다
John
6969
다시 setProperty 태그만 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ 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 class="com.edu.beans.HelloBeans" id="hello"/> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name="hello"/><br> <jsp:setProperty property="*" name="hello"/> <hr> <jsp:getProperty property="name" name="hello"/><br> <jsp:getProperty property="number" name= "hello"/><br> </body> </html> | cs |
http://localhost:8081/edu/example17.jsp?name=Tom&number=99999
원래 변수의 값대로 질의 문자열을 전송
이름이 없습니다
번호가 없습니다
Tom
99999