디렉티브 (1)

💻 Programming/JSP

[JSP] Directives ( 디렉티브 )

JSP directives 는 container에 방향이나 명령을 제공합니다. 특정 부분에 대해서 어떻게 처리를 해야 한다는 것을 말이죠

JSP directive는 servlet class 전반에 걸쳐서 영향을 미칩니다.  

문법은 아래와 같습니다. 

 

<%@ directive attribute="value" %>

Directives 는 여러개의 속성을 가질 수 있습니다. 속성과 속성 사이는 쉼표로 구분을 하고 key-value 페어로 값을 지정할 수 있습니다. 

 @ 와 directive명 사이의 빈칸 그리고 마지막 속성과 %> 사이의 빈칸은 옵션입니다. 있어도 되고 없어도 된다는 거죠. 

 

디렉티브 태그는 총 세가지 종류가 있습니다.  

DirectiveDescription
<%@ page ... %>페이지 종속적인 속성들을 정의합니다. scripting language, error page, and buffering requirements 같은 것들을 말합니다.
<%@ include ... %>로드될 때 동적으로 파일을 include합니다.
<%@ taglib ... %>페이지에서 사용될 사용자정의 액션을 포함하는 태그 라이브러리를 선언합니다. 


The page Directive

page directive 는 현재 JSP 페이지와 관련된 명령어들을 컨테이너에 제공합니다. 또한 페이지 디렉티브는 JSP페이지 아무데나 넣어도 관계가 없습니다. 하지만 일반적으로 JSP페이지의 가장 위쪽에 정의합니다.

기본문법은 아래와 같습니다.

<%@ page attribute="value" %>

XML형식은 아래와 같습니다.

<jsp:directive.page attribute="value" />

Attributes ( 속성 )

페이지 디렉티브와 관련된 속성과 목적은 아래와 같습니다. 

AttributePurpose
bufferSpecifies a buffering model for the output stream.
autoFlushControls the behavior of the servlet output buffer.
contentTypeDefines the character encoding scheme.
errorPageDefines the URL of another JSP that reports on Java unchecked runtime exceptions.
isErrorPageIndicates if this JSP page is a URL specified by another JSP page's errorPage attribute.
extendsSpecifies a superclass that the generated servlet must extend
importSpecifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes.
infoDefines a string that can be accessed with the servlet's getServletInfo() method.
isThreadSafeDefines the threading model for the generated servlet.
languageDefines the programming language used in the JSP page.
sessionSpecifies whether or not the JSP page participates in HTTP sessions
isELIgnoredSpecifies whether or not EL expression within the JSP page will be ignored.
isScriptingEnabledDetermines if scripting elements are allowed for use.

속성에 대한 자세한 내용은 이곳에서 확인하실 수 있습니다. 


The include Directive:

include directive는 페이지가 로드될 때 동적으로 다른 파일을 include시키기 위해서 사용됩니다. 쉽게말하면 다른 페이지에 따로 기능을 구현해놓고 이것을 필요로 하는 JSP페이지에서 include하여 해당 기능을 사용하기 위해서 있는겁니다. 

역시 위치는 JSP페이지 내 아무곳에나 가져다 놓으시면 됩니다. 

문법은 아래와 같습니다. 

<%@ include file="relative url" >

file속성의 value로 들어가는 값은 파일의 상대경로라는 점을 주의하시면 됩니다.  

 

XML형식은 아래와 같습니다.

<jsp:directive.include file="relative url" />

Example:

예제를 한번 볼까요? ROOT 디렉토리에 아래처럼 header, footer, main JSP파일을 만들어 봅시다.

 

header.jsp

<%! int pageCount = 0; void addCount() { pageCount++; } %> <% addCount(); %> <html> <head> <title>The include Directive Example</title> </head> <body> <center> <h2>The include Directive Example</h2> <p>This site has been visited <%= pageCount %> times.</p> </center> <br/><br/>

footer.jsp

<br/><br/> <center> <p>Copyright © 2010</p> </center> </body> </html>

main.jsp

<%@ include file="header.jsp" %> <center> <p>Thanks for visiting my page.</p> </center> <%@ include file="footer.jsp" %>

그리고 main.jsp를 웹브라우저에서 요청을 합니다. 그러면 아래와 같은 결과가 나올것입니다. main.jsp에서는 include로 다른 jsp파일만 추가를 했을 뿐인데 눈에 보이는게 많죠? include된 jsp파일들에서 뿌리는 결과가 main에 모두 합쳐져서 나오게 되는 것입니다. 

The include Directive Example

This site has been visited 1 times.



Thanks for visiting my page.



Copyright © 2010

 

새로고침할 때마다 방문 카운트가 증가하는게 보이시나요?? ^___^ 

 


The taglib Directive:

태그 라이브러리는 사용자 정의 태그입니다.  

taglib directive는 JSP페이지에서 사용자 정의 태그를 사용하겠다고 정의하기 위해 사용합니다.

 

문법은 아래와 같습니다.

<%@ taglib uri="uri" prefix="prefixOfTag" >

XML형식은 아래와 같습니다.

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />

Example:

예제를 한번 봅시다. 

 custlib tag library 가 hello 태그를 가지고 있다고 합시다. hello tag 를 mytag​라는 prefix와 함께 사용하고 싶을 때(  <mytag:hello> ) 는 아래처럼 사용할 수 있습니다.

<%@ taglib uri="http://www.example.com/custlib" prefix="mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</html>

 <mytag:hello> 를 호출함으로써 hello 태그에 미리 정의되어있는 코드를 써먹을 수 있는거죠.

 

그럼 hello 태그가 어떻게 정의되어있는지 한번 볼까요?

 

지금 말고 다음 포스팅에서 보도록 하죠. 생각보다 내용이 많거든요 ㅋㅋ 참고로 여기서 사용된 hello 태그를 커스텀 태그라고 합니다.

 

 

 

그럼 좋은 하루 되세요 ^___^

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_directives.htm 

'💻 Programming > JSP' 카테고리의 다른 글

[JSP] Actions ( 액션 )  (0) 2019.02.15
[JSP] Custom Tags ( 커스텀, 사용자 정의 태그 )  (0) 2019.02.15
[JSP] JSP 생명 주기 ( life cycle )  (0) 2019.02.15
[JSP] 조건문, 반복문  (0) 2019.02.15
[JSP] JSP 시작하기  (0) 2019.02.15