본문 바로가기
📌 Front End/└ JSP

[JSP] <%@ include %>디렉티브 <jsp:include>액션태그 차이점

by 쫄리_ 2024. 5. 21.
728x90
반응형

1. 스크립트 태그 (include 디렉티브)

<%@ include file="" %>

 

- jsp 파일과 구분하기 위해 JSP Fragment 를 사용한다.(jspf)

include 디렉티브 (jspf)
include 디렉티브는 해당 file을 include 디렉티브를 호출한 위치에 먼저 적용시키는 개념이다.
따라서 include 디렉티브의 file은 전체적인 jsp의 구조를 갖추지 않고 조각형태로 가져도 된다.
이러한 조각형태로 존재하는 jsp파일은 확장자로 "jspf"를 설정해주면 된다.

2. 액션 태그 (jsp 액션 태그)

<jsp:include page="" />
<jsp:include page = "포함할 다른 페이지" flush = "true" />

3. <jsp:include>  vs  include디렉티브 비교

비교 <jsp:include> inclue 디렉티브
처리시간 코드 내에서 <jsp:include> 요청 시 처리 JSP 파일을 자바 소스코드로
변환할 때 처리
기능 아예 별도의 파일로
<jsp:include> 에 지정된 파일에서 
실행한 결과를 현재 JSP 페이지에 표현
현재 파일에 include디렉티브에
저장된 파일의 코드를 삽입
데이터 전달 방법 request 기본 객체나
<jsp:param> 을 이용한 파라미터로 value 전달
include 디렉티브에 지정된 파일 내에
변수 등을 지정하고
현재 JSP에서 "include 디렉티브" 가
선언된 다음 줄 부터 해당 변수 사용
용도 화면의 레이아웃의 일부분을
모듈화할 때 주로 사용
다수의 JSP 페이지에서
공통으로 사용되는 코드나
저작권과 같은 문장을 포함

include 디렉티브 - 사용예시

include_test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int num = 100;
%>
 
<%@ include file="include_file.jsp" %>
<br>
 
include_file.jsp의 변수 "int num2" : <%=num2%>
</body>
</html>
"include_file.jsp"에서는 include_test.jsp에서 선언된 변수 "int num = 100"을 사용할 수 있다.

 

 

include_file.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 
<%
    int num2 = 500;
%>
include_test.jsp의 변수 "int num" : <%=num%>
</body>
</html>
"include_test.jsp"에서는 include_file.jsp에서 선언된 변수 "int num2 = 500"을 사용할 수 있다

이러한 기능(A라는 jsp파일의 필드를 B라는 jsp파일에서 사용)이 가능한 이유는
<% include> = include 디렉티브는 "file에 걸려있는 jsp파일"을 먼저 현재 위치에 포함시키고
자바 소스코드로 변환한 다음에 서블릿 클래스로 컴파일하기 때문이다

 


4. web.xml 이용한 파일 코드 자동 삽입

<jsp-config>
    <jsp-property-group>
        <url-pattern>"property에 적용할 JSP 파일의 URL 패턴"</url-pattern>
        <include-prelude>"앞에 삽입할 파일"</include-prelude>
        <include-coda>"뒤에 삽입할 파일"</include-coda>
    </jsp-property-group>
</jsp-config>
include 디렉티브를 사용하지 않고,
web.xml 파일을 이용해서 "현재 JSP 페이지"의 앞/뒤에 지정한 파일을 삽입할 수 있다.

사용예시 1

web.xml

<jsp-config>
    <jsp-property-group>
        <url-pattern>"property에 적용할 JSP 파일의 URL 패턴"</url-pattern>
        <include-prelude>header.jsp</include-prelude>
        <include-coda>footer.jsp</include-coda>
    </jsp-property-group>
</jsp-config>

 

적용.jsp

<%@ include file = "header.jsp" %>
<html>
..
..
..
<%@ include file = "footer.jsp" %>

 

 

728x90
반응형