JSP/Servlet/4. 페이지 모듈화와 요청 흐름 제어

Day 53 : include 디렉티브를 이용한 중복된 코드 삽입

pancakemaker 2021. 12. 30. 15:19

1. include 디렉티브

: 지정한 페이지를 현재 위치에 포함시키는 기능

 

※ <jsp:include>와 include 디렉티브의 차이

- <jsp:include> 액션 태그 : 다른 JSP로 실행 흐름을 이동시켜 실행 결과를 현재 위치에 포함하는 방식

구성 요소 모듈화

- include 디렉티브 : 다른 파일의 내용을 현재 위치에 삽입한 후에 JSP 파일을 자바 파일로 변환하고 컴파일하는 방식

→ 모든 JSP 페이지에서 사용하는 변수 지정, 저작권 표시와 같이 모든 페이지에서 중복되는 간단한 문장


2. include 디렉티브의 처리 방식과 사용법

<%@ include file="포함할파일" %>

 

- includee.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
includer.jsp에서 지정한 번호: <%= number %>
<p>
<% 
	String dataFolder = "c:\\data";
%>

- includer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>include 디렉티브</title>
</head>
<body>
<%
	int number = 10;
%>

<%@ include file = "/includee.jspf" %>

공통변수 DATAFOLDER = "<%= dataFolder %>"
</body>
</html>


3. 코드 조각 자동 포함 기능

: include 디렉티브 사용 없이 지정한 파일 삽입 기능

: web.xml 에 별도 코드 작성 필요

 

- web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
		http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
		version="3.1">
		
	<jsp-config>
		<jsp-property-group>
			<url-pattern>/view/*</url-pattern>
				<include-prelude>/common/variable.jspf</include-prelude>
				<include-coda>/common/footer.jspf</include-coda>
		</jsp-property-group>	
	</jsp-config>
	
</web-app>

- chap07\common\variable.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	java.util.Date CURRENT_TIME = new java.util.Date();
%>

- chap07\common\footer.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 소스코드작성: kosmo -->

- chap07\view\autoInclude.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>자동 Include 실행</title>
</head>
<body>

현재 시간은 <%= CURRENT_TIME %> 입니다.

</body>
</html>