λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JSP

[JSP] μ•‘μ…˜ νƒœκ·Έ

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 10.
λ°˜μ‘ν˜•

β–Ά μ•‘μ…˜ νƒœκ·Έ

μ•‘μ…˜ νƒœκ·Έλž€ JSP νŽ˜μ΄μ§€μ—μ„œ μžλ°” μ½”λ“œ λ“±μ˜ 슀크립트 μ–Έμ–΄λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³ λ„(즉, HTML νƒœκ·Έ ν˜•νƒœλ‘œ) λ‹€λ₯Έ νŽ˜μ΄μ§€μ˜ μ„œλΈ”λ¦Ώμ΄λ‚˜ μžλ°”λΉˆμ˜ 객체에 μ ‘κ·Όν•  수 μžˆλ„λ‘ νƒœκ·Έλ₯Ό μ΄μš©ν•΄ κ΅¬ν˜„λœ κΈ°λŠ₯을 λ§ν•œλ‹€. λ˜ν•œ μ΄λŸ¬ν•œ κΈ°λŠ₯듀은 μŠ€ν¬λ¦½ν‹€λ¦Ώ λ“±μ˜ 슀크립트 μš”μ†Œ(μžλ°” μ½”λ“œ)λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— κ°œλ°œμžλŠ” JSP νŽ˜μ΄μ§€μ˜ 내뢀적인 ν”„λ‘œκ·Έλž¨ λ‘œμ§μ„ μ‚¬μš©μžλ‘œλΆ€ν„° 감좜 μˆ˜κ°€ μžˆλ‹€.

 

JSPμ—μ„œ μ œκ³΅ν•˜λŠ” μ•‘μ…˜ νƒœκ·ΈλŠ” 크게 λ‹€μŒκ³Ό 같이 λ‚˜λˆŒ 수 μžˆλ‹€.

-νŽ˜μ΄μ§€ 프름 μ œμ–΄ μ•‘μ…˜(forward/include μ•‘μ…˜)

-μžλ°”λΉˆ μ‚¬μš© μ•‘μ…˜(useBean μ•‘μ…˜)

-μ• ν”Œλ¦Ώ μ‚¬μš© μ•‘μ…˜(plugin μ•‘μ…˜) ← 잘 μ‚¬μš© μ•ˆν•¨

​

​

​

β–Ά forward μ•‘μ…˜

forward μ•‘μ…˜μ€ pageContext λ‚΄μž₯ 객체의 forward() λ©”μ„œλ“œκ°€ νƒœκ·Έλ‘œ κ΅¬ν˜„λœ κΈ°λŠ₯이닀. λ”°λΌμ„œ forward μ•‘μ…˜μ€ ν˜„μž¬ νŽ˜μ΄μ§€μ˜ μš”μ²­κ³Ό 응닡에 κ΄€ν•œ μ²˜λ¦¬κΆŒμ„ page 속성에 μ§€μ •λœ 이동할 νŽ˜μ΄μ§€λ‘œ 영ꡬ적으둜 λ„˜κΈ°λŠ” κΈ°λŠ₯을 ν•œλ‹€. μ‚¬μš©μžκ°€ request에 μ§€μ •ν•œ 속성듀은 ν¬μ›Œλ”©λœ νŽ˜μ΄μ§€μ—μ„œλ„ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

forward μ•‘μ…˜μ˜ page μ†μ„±μ—μ„œ μ§€μ •λ˜λŠ” 이동할 νŽ˜μ΄μ§€μ˜ μ£Όμ†ŒλŠ” λ™μΌν•œ μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ μ»¨ν…μŠ€νŠΈ 루트λ₯Ό κΈ°μ€€μœΌλ‘œ ν•œ μ ˆλŒ€ κ²½λ‘œλ‚˜ μƒλŒ€ 경둜둜만 지정이 κ°€λŠ₯ν•˜λ‹€.

즉, page 속성을 지정할 λ•ŒλŠ” μŠ€ν‚€λ§ˆ(http://) λΆ€λΆ„κ³Ό 도메인(localhost) λΆ€λΆ„ 그리고 포트 번호λ₯Ό μƒλž΅ν•΄μ•Ό ν•œλ‹€.

​

​

forwardTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Forward Action Test</title>
</head>
<body>
<h2>ν¬μ›Œλ“œ μ•‘μ…˜ ν…ŒμŠ€νŠΈ</h2>
<form action="forwardTest1.jsp" method="post">
<input type="hidden" name="forwardPage" value="forwardTest2.jsp">
<table>
	<tr>
		<td>이름     </td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>λ‚˜μ΄     </td>
		<td><input type="text" name="age"></td>
	</tr>
	<tr>
		<td>μ£Όμ†Œ     </td>
		<td><input type="text" name="address"></td>
	</tr>
	<tr><td><input type="submit" value="전솑"></td></tr>
</table>
</form>
</body>
</html>

 

 

forwardTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page='<%=request.getParameter("forwardPage") %>'>
	<jsp:param name="tel" value="034-1234-5678"/>
</jsp:forward>
</body>
</html>

 

 

forwardTest2.jsp

<%@ 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>
<h2>ν¬μ›Œλ“œ 된 νŽ˜μ΄μ§€(forwardTest2.jsp)</h2>
<table>
	<tr>
		<td>이름</td>
		<td><%=request.getParameter("name") %></td>
	</tr>
	<tr>
		<td>λ‚˜μ΄</td>
		<td><%=request.getParameter("age") %></td>
	</tr>
	<tr>
		<td>μ£Όμ†Œ</td>
		<td><%=request.getParameter("address") %></td>
	</tr>
	<tr>
		<td>μ „ν™”λ²ˆν˜Έ</td>
		<td><%=request.getParameter("tel") %></td>
	</tr>
</table>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

 

 

 

β–Ά include μ•‘μ…˜ νƒœκ·Έ

include μ•‘μ…˜μ€ μž„μ‹œλ‘œ μ œμ–΄κΆŒμ„ includeλ˜λŠ” νŽ˜μ΄μ§€λ‘œ λ„˜κ²Όλ‹€κ°€ κ·Έ νŽ˜μ΄μ§€μ˜ μ²˜λ¦¬κ°€ λλ‚˜λ©΄ 처리 κ²°κ³ΌλŠ” μ›λž˜ νŽ˜μ΄μ§€λ‘œ λ˜λŒμ•„μ˜€κ³  λ‹€μ‹œ μ›λž˜μ˜ νŽ˜μ΄μ§€λ‘œ μ œμ–΄κΆŒμ„ λ°˜ν™˜ν•˜λŠ” 방식이닀. λ”°λΌμ„œ include μ•‘μ…˜μ€ include μ§€μ‹œμ–΄μ²˜λŸΌ μ—¬λŸ¬ νŽ˜μ΄μ§€λ₯Ό ν•œ νŽ˜μ΄μ§€ μ•ˆμ— λ‹΄λŠ” κΈ°λŠ₯을 κ°–κ³  μžˆλ‹€.

include μ•‘μ…˜μ€ μ—¬λŸ¬ νŽ˜μ΄μ§€λ₯Ό λ™μ μœΌλ‘œ ν•˜λ‚˜μ˜ νŽ˜μ΄μ§€λ‘œ 묢을 수 μžˆμœΌλ―€λ‘œ 각각의 νŽ˜μ΄μ§€λ₯Ό κΈ°λŠ₯λ³„λ‘œ λͺ¨λ“ˆν™”μ‹œμΌœμ„œ ν•˜λ‚˜μ˜ νŽ˜μ΄μ§€λ₯Ό μ—¬λŸ¬ λͺ¨λ“ˆν™”λœ νŽ˜μ΄μ§€μ˜ μ§‘ν•©μœΌλ‘œ ν‘œν˜„ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ©° μ΄λŸ¬ν•œ νŽ˜μ΄μ§€λ₯Ό ν…œν”Œλ¦Ώ νŽ˜μ΄μ§€λΌκ³  ν•œλ‹€.

​

​​

includeTest1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Include Action Test</title>
</head>
<body>
<h2>μΈν΄λ£¨λ“œ μ•‘μ…˜ ν…ŒμŠ€νŠΈ</h2>
<jsp:include page="includeTest2.jsp">
	<jsp:param name="name" value="hongkildong"/>
</jsp:include>
</body>
</html>

 

 

includeTest2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String name = request.getParameter("name");
%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<b><%=name %></b>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

​

​

​

β–Ά XMLElementλ₯Ό μƒμ„±ν•˜λŠ” μ•‘μ…˜ νƒœκ·Έλ“€

JSPμ—μ„œ μ œκ³΅ν•˜λŠ” XML μ—˜λ¦¬λ¨ΌνŠΈ κ΄€λ ¨ μ•‘μ…˜ νƒœκ·Έλ“€μ€ JSP 내에 XML κ΄€λ ¨ μ—˜λ¦¬λ¨ΌνŠΈλ“€μ„ λ™μ μœΌλ‘œ μƒμ„±ν•˜λŠ” 역할을 ν•œλ‹€.

<jsp:element>

<jsp:attribute>

<jsp:body>

​

​

xmlGenerator.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ 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:element name="member">
<jsp:attribute name="id">
	member1
</jsp:attribute>
<jsp:body>
	μ˜€μ •μ›
</jsp:body>
</jsp:element>
</body>
</html>

 

<μ‹€ν–‰ κ²°κ³Ό>

xml μ—˜λ¦¬λ¨ΌνŠΈκ°€ μƒμ„±λœ ν™”λ©΄

λ°˜μ‘ν˜•

λŒ“κΈ€