본문 바로가기
프로그래밍/jsp

[JSP2.3] 페이지 모듈화 와 요청흐름 제어 - 1

by 혀끄니 2024. 2. 26.
728x90

<jsp:include>액션 태그를 이용한 공통 영역 작성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
메인이당

<jsp:include page="sub.jsp" flush="false"/>

include 이후의 내용
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
서브당
</body>
</html>

 

<jsp:param>으로 포함할 페이지에 파라미터 추가하기

<jsp:include page="/module/top.jsp" flush="false">
  <jsp:param name="param1" value="value1"/>
  <jsp:param name="param2" value="value2"/>
</jsp:include>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
	request.setCharacterEncoding("utf-8"); 
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
include전 name 파리미터 값: <%= request.getParameter("name") %>

<hr>

<jsp:include page="body_sub.jsp" flush="false">
	<jsp:param value="김짱구" name="name"/>
</jsp:include>
<hr/>
include 후 name파라미터 값 <%=request.getParameter("name") %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
body_sub에 name파라미터 값: <%=request.getParameter("name")%>
<br/>
name 파라미터 값 목록 :
<ul>
	<%
		String[] names = request.getParameterValues("name");
		for(String name: names){
	%>
		<li> <%=name %></li>
	<%
		}
	%>
</ul>
</body>
</html>

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

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
	int number =10;
%>
<%@ include file="/chap07/includee.jsp" %>

공통변수 DATAFOLDER = "<%= dataFolder %>"
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
include.jsp에서 지정한 번호:<%=number %>
<p>
<%
String dataFolder = "c:\\data";
%>
</body>
</html>

 

코드 조각 자동 포함 가능

<jsp-config>
  <jsp-property-group>
   <url-pattern>/view/*</url-pattern>
   <include-prelude>/common/variable.jsp</include-prelude>
   <include-coda>/common/footer.jsp</include-coda>
  </jsp-property-group>
</jsp-config>

 

-<jsp-property-group>:JSP의 프로퍼티를 포함

-<url-pattern>:프로퍼티를 적용할 JSP파일의 URL 패턴을 지정한다.

-<include-prelude>:url-pattern 태그에 지정한 패턴에 해당하는 JSP파일의 앞에 삽입할 파일을 지정한다.

-<include-coda>:url-pattern 태그에 지정한 패턴에 해당하는 JSP파일의 뒤에 삽입할 파일을 지정한다.

비교항목
<jsp:include>
include 디렉티브
처리 시간
요청 시간에 처리
JSP파일을 자바 소스로 변환 할때 처리
기능
별도의 파일로 요청 처리 흐름을 이동
현재 파일에 삽입 시킴
데이터 전달 방법
request 기본 객체난 <jsp:param>을 이용한 파라미터 전달
페이지 내의 변수를 선언한 후, 변수에 값 저장
용도
화면의 레이아웃의 일부분을 모듈화할 때 주로 사용된다.
다수의 JSP 페이지에서 공통으로 사용되는 변수를 지정하는 코드나 저작권과 같은 문장을 포함한다.

728x90