Search

JSP ํƒœ๊ทธ

JSP ํƒœ๊ทธ

โ€ข
์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ : JSP ์—์„œ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํƒœ๊ทธ
โ€ข
๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ : JSP ํŽ˜์ด์ง€์˜ ์†์„ฑ๊ณผ ๊ตฌ์„ฑ์„ ์„ค์ •ํ•˜๋Š” ํƒœ๊ทธ
โ€ข
์•ก์…˜ ํƒœ๊ทธ : JSP ํŽ˜์ด์ง€์—์„œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํƒœ๊ทธ

์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ

โ€ข
์„ ์–ธ๋ฌธ : <%! ์ฝ”๋“œ %> : ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ ์ •์˜
โ€ข
์Šคํฌ๋ฆฝํ‹€๋ฆฟ : <% ์ฝ”๋“œ %> : ์ž๋ฐ” ์ฝ”๋“œ ๋กœ์ง
โ€ข
ํ‘œํ˜„๋ฌธ : <%= ์ฝ”๋“œ %> : ๋ณ€์ˆ˜, ๊ณ„์‚ฐ ์ˆ˜์‹, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
ํƒœ๊ทธ
ํ˜•ํƒœ
์˜๋ฏธ
์„ ์–ธ๋ฌธ
<%! %>
๋ณ€์ˆ˜ ์„ ์–ธ O, ๋ฉ”์†Œ๋“œ ์ •์˜ O
์Šคํฌ๋ฆฝํ‹€๋ฆฟ
<% %>
๋ณ€์ˆ˜ ์„ ์–ธ O, ๋ฉ”์†Œ๋“œ ์ •์˜ X
ํ‘œํ˜„๋ฌธ
<%= %>
๋ณ€์ˆ˜ ์‚ฌ์šฉ, ์ˆ˜์‹ ๊ณ„์‚ฐ, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ

์„ ์–ธ๋ฌธ ๋ณ€์ˆ˜ ์„ ์–ธ VS ์Šคํฌ๋ฆฝํ‹€๋ฆฟ ๋ณ€์ˆ˜ ์„ ์–ธ

โ€ข
์„ ์–ธ๋ฌธ์—์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ : ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜ ์‹œ, _jspService() ๋ฉ”์†Œ๋“œ ์™ธ๋ถ€์— ์„ ์–ธ
โ€ข
์Šคํฌ๋ฆฝํ‹€๋ฆฟ ๋ณ€์ˆ˜ ์„ ์–ธ : ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜ ์‹œ, _jspService() ๋ฉ”์†Œ๋“œ ๋‚ด๋ถ€์— ์„ ์–ธ

๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ

ํƒœ๊ทธ
ํ˜•์‹
์˜๋ฏธ
page
<%@ page โ€ฆ %>
JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ •๋ณด ์„ค์ •
include
<%@ include โ€ฆ %>
์™ธ๋ถ€ JSP ํŽ˜์ด์ง€๋ฅผ ํŽ˜์ด์ง€ ๋‚ด๋ถ€๋กœ ํฌํ•จ์‹œํ‚ด
taglib
<%@ taglib โ€ฆ %>
JSP ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•  ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์ •

page ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ

์†์„ฑ (Attribute)
์„ค๋ช…
๊ธฐ๋ณธ๊ฐ’
language
JSP ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•  ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
java
contentType
์‘๋‹ต์˜ ์ฝ˜ํ…์ธ  ํƒ€์ž…๊ณผ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
text/html
pageEncoding
JSP ํŽ˜์ด์ง€์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
ISO-8859-1
import
๋‹ค๋ฅธ ํด๋ž˜์Šค๋‚˜ ํŒจํ‚ค์ง€๋ฅผ JSP ํŽ˜์ด์ง€๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
session
true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. true๋กœ ์„ค์ •ํ•˜๋ฉด JSP ํŽ˜์ด์ง€์—์„œ ์„ธ์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
true
buffer
๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
8KB
autoFlush
true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. true๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฒ„ํผ๊ฐ€ ๊ฐ€๋“ ์ฐจ๋ฉด ์ž๋™์œผ๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
true
isThreadSafe
true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. true๋กœ ์„ค์ •ํ•˜๋ฉด JSP ํŽ˜์ด์ง€๊ฐ€ ์Šค๋ ˆ๋“œ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
true
info
JSP ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
errorPage
์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  URL์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
isErrorPage
true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. true๋กœ ์„ค์ •ํ•˜๋ฉด ์ด ํŽ˜์ด์ง€๊ฐ€ ์˜ค๋ฅ˜ ํŽ˜์ด์ง€์ž„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
false
isELIgnored
true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. true๋กœ ์„ค์ •ํ•˜๋ฉด EL (ํ‘œํ˜„ ์–ธ์–ด)์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.
false
extends
JSP ํŽ˜์ด์ง€์˜ ํด๋ž˜์Šค๊ฐ€ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

pageEncoding ์†์„ฑ

โ€ข
ํ•œ๊ธ€ ์ถœ๋ ฅ์„ ์œ„ํ•ด ์ง€์ •ํ•ด์•ผํ•˜๋Š” ์†์„ฑ๊ฐ’
โ—ฆ
UTF-8
โ—ฆ
EUC-KR
์œ„์˜ UTF-8, EUC-KR ์ธ์ฝ”๋”ฉ ํƒ€์ž…์ด ์•„๋‹Œ ๊ธฐ๋ณธ๊ฐ’ ISO-8859-1 ์ด ์ ์šฉ๋˜์–ด ์žˆ์œผ๋ฉด, ํ•œ๊ธ€์ด ๊นจ์ง
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
HTML
๋ณต์‚ฌ
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
HTML
๋ณต์‚ฌ

include ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ

: JSP ์— ์™ธ๋ถ€ ํŒŒ์ผ์„ ํฌํ•จ์‹œํ‚ค๋Š” ํƒœ๊ทธ
ํฌํ•จ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ : HTML, JSP, TXT ํŒŒ์ผ ๋“ฑ

์‹ค์Šต ํŒŒ์ผ

โ€ข
include.jsp
โ€ข
header.jsp
โ€ข
footer.jsp
โ€ข
copy.txt

include.jsp

<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>include</title> </head> <body> <%@ include file="include01_header.jsp" %> <div class="container"> <h1>์ปจํ…์ธ  ์˜์—ญ</h1> <h3><%= new Date() %></h3> </div> <%@ include file="include01_footer.jsp" %> </body> </html>
HTML
๋ณต์‚ฌ

header.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> <header> <h1>ํ—ค๋” ์˜์—ญ</h1> </header> </body> </html>
HTML
๋ณต์‚ฌ

footer.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> <footer> <p><%@ include file="copy.txt" %></p> </footer> </body> </html>
HTML
๋ณต์‚ฌ

copy.txt

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Copyright โ“’์•Œ๋กœํ•˜ํด๋ž˜์Šค. All Rights Reserved
HTML
๋ณต์‚ฌ

taglib ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ

: JSP ์—์„œ JSTL, EL(ํ‘œํ˜„ ์–ธ์–ด), ์ปค์Šคํ…€ ํƒœ๊ทธ ๋“ฑ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์ •ํ•˜๋Š” ํƒœ๊ทธ

JSTL ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

JSTL (JavaServer Pages Standard Tag Library)

: Java ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ์ค€ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Tomcat 10 ๋ฒ„์ „ ๋ฏธ๋งŒ์—์„œ ์‚ฌ์šฉ

1.
JSTL ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ
a.
taglibs-standard-compat-1.2.5.jar
b.
taglibs-standard-impl-1.2.5.jar
c.
taglibs-standard-jstlel-1.2.5.jar
d.
taglibs-standard-spec-1.2.5.jar
2.
JSP ํ”„๋กœ์ ํŠธ ์— ํฌํ•จํ•˜๊ธฐ
a.
[WEB-INF] > [lib] ํด๋”์— ์ถ”๊ฐ€
JSTL ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๊ธฐ
3.
JSP ์ฝ”๋“œ ์ƒ๋‹จ์— taglib ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ ์ถ”๊ฐ€
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
JavaScript
๋ณต์‚ฌ
4.
์ฝ”๋“œ ์ž‘์„ฑ
<%-- taglib ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ ์ถ”๊ฐ€ --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>taglib ๋””๋ ‰ํ‹ฐ๋ธŒ - JSTL</title> </head> <body> <%-- WEB-INF > lib : jstl-xxx-impl.jar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ --%> <c:forEach var="k" begin="1" end="10" step="1"> <c:out value="${k}" /> </c:forEach> </body> </html>
JavaScript
๋ณต์‚ฌ

Tomcat 10 ๋ฒ„์ „ ์ด์ƒ์—์„œ ์‚ฌ์šฉ

โ€ข
jstl
โ€ข
jstl-api
โ€ข
servlet-api

jstl

jstl-api

servlet-api

์•ก์…˜ ํƒœ๊ทธ

: ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํƒœ๊ทธ
โ€ข
forward : ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋Š” ํƒœ๊ทธ
โ€ข
include : ์™ธ๋ถ€ ํŒŒ์ผ์„ ํฌํ•จํ•˜๋Š” ํƒœ๊ทธ
โ€ข
param : ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ๋„˜๊ฒจ์ค„ ํŒŒ๋ผ๋ฏธํ„ฐ(๋งค๊ฐœ๋ณ€์ˆ˜)๋ฅผ ์ง€์ •ํ•˜๋Š” ํƒœ๊ทธ
โ€ข
์ž๋ฐ”๋นˆ์ฆˆ ์•ก์…˜ ํƒœ๊ทธ
์•ก์…˜ ํƒœ๊ทธ
์„ค๋ช…
<jsp:include>
๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค๋ฅผ ํ˜„์žฌ ํŽ˜์ด์ง€์— ํฌํ•จ์‹œํ‚ต๋‹ˆ๋‹ค.
<jsp:forward>
ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์ œ์–ด๋ฅผ ๋‹ค๋ฅธ ์›น ์ž์›์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
<jsp:param>
<jsp:include> ๋˜๋Š” <jsp:forward>์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
<jsp:useBean>
JavaBeans ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
<jsp:setProperty>
JavaBeans ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
<jsp:getProperty>
JavaBeans ๊ฐ์ฒด์˜ ์†์„ฑ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
<jsp:plugin>
๋ธŒ๋ผ์šฐ์ € ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ” ์• ํ”Œ๋ฆฟ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
<jsp:element>
XML ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
<jsp:attribute>
์ปค์Šคํ…€ ํƒœ๊ทธ์˜ ์†์„ฑ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
<jsp:text>
ํ…์ŠคํŠธ ๋ธ”๋ก์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

forward ์•ก์…˜ํƒœ๊ทธ

<jsp:forward page="์ด๋™ํ• ํŒŒ์ผ๋ช….jsp" />
HTML
๋ณต์‚ฌ

ํŽ˜์ด์ง€ ์ด๋™ ๋ฐฉ์‹

โ€ข
forward
โ€ข
redirect
์›น ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹์€ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„์–ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

forward ๋ฐฉ์‹

forward : โ€œ์ „๋‹ฌํ•˜๋‹คโ€
โ€ข
์„œ๋ฒ„ ๋‚ด๋ถ€์—์„œ ์š”์ฒญ ๊ฒฝ๋กœ A ๊ฐ€ B ๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋ฉฐ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹
โ€ข
ํŠน์ง•
โ—ฆ
์›๋ž˜์˜ ์š”์ฒญ ์ •๋ณด(request) ๋ฐ ์‘๋‹ต ์ •๋ณด(response)๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
โ—ฆ
ํด๋ผ์ด์–ธํŠธ๋Š” ์ด๋™ํ•œ ํŽ˜์ด์ง€ B ๋ฅผ ์‘๋‹ต ๋ฐ›์ง€๋งŒ, ์ฃผ์†Œ ํ‘œ์‹œ์ค„์˜ ์š”์ฒญ URL ์€ ๊ทธ๋Œ€๋กœ์ž„
โ—ฆ
์กฐํšŒ(์ฝ๊ธฐ) ์š”์ฒญ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ฃผ๋กœ ์‚ฌ์šฉ

redirect ๋ฐฉ์‹

redirect : โ€œ๋‹ค์‹œ ์ง€์‹œํ•˜๋‹คโ€
โ€ข
์„œ๋ฒ„๊ฐ€ ์š”์ฒญ ๊ฒฝ๋กœ A ๋กœ ์š”์ฒญ์„ ๋ฐ›์€ ํ›„, ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ B ๋กœ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ผ๊ณ  ์ง€์‹œํ•˜๋ฉฐ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹
โ€ข
ํŠน์ง•
โ—ฆ
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›๋ž˜์˜ ์š”์ฒญ๊ณผ ๋‹ฌ๋ฆฌ ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ์š”์ฒญ์„ ํ•˜์—ฌ, ๊ธฐ์กด์˜ ์š”์ฒญ ์ •๋ณด๋Š” ์ดํ›„์˜ ์š”์ฒญ์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
โ—ฆ
์ด๋™ํ•œ ํ™”๋ฉด์—์„œ ์ฃผ์†Œ ํ‘œ์‹œ์ค„์˜ URL ์ด A ์—์„œ B ๋กœ ๋ณ€ํ™”๋จ
โ—ฆ
๋“ฑ๋ก, ์ˆ˜์ •, ์‚ญ์ œ ์š”์ฒญ ์‹œ์— ์ฃผ๋กœ ์‚ฌ์šฉ

์‹ค์Šต ํŒŒ์ผ

โ€ข
forward_A.jsp
โ€ข
forward_B.jsp

forward_A.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Action Tag</title> </head> <body> <h2>forward A ํ™”๋ฉด</h2> <jsp:forward page="forward_B.jsp" /> <p>-------------------------------</p> </body> </html>
HTML
๋ณต์‚ฌ

forward_B.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Action Tag</title> </head> <body> <h2>forward B ํ™”๋ฉด</h2> </body> </html>
HTML
๋ณต์‚ฌ

include ์•ก์…˜ํƒœ๊ทธ

: ์™ธ๋ถ€ ํŒŒ์ผ์„ ํฌํ•จํ•˜๋Š” ํƒœ๊ทธ
ํฌํ•จ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ : HTML, JSP, ์„œ๋ธ”๋ฆฟ ํŽ˜์ด์ง€ ๋“ฑ
๋Ÿฐํƒ€์ž„์— ํฌํ•จ๋˜๋ฉฐ, ํฌํ•จ๋˜๋Š” JSP ํŽ˜์ด์ง€๊ฐ€ ํ˜„์žฌ ํŽ˜์ด์ง€์™€ ๋ณ„๊ฐœ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰
<jsp:include page="ํฌํ•จํ• ํŒŒ์ผ๋ช….jsp" flush="false" />
HTML
๋ณต์‚ฌ

์‹ค์Šต ํŒŒ์ผ

โ€ข
include_main.jsp
โ€ข
include_header.jsp
โ€ข
include_footer.jsp

include_main.jsp

<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>include</title> </head> <body> <jsp:include page="include_header.jsp" flush="false" /> <div class="container"> <h1>์ปจํ…์ธ  ์˜์—ญ</h1> <h3><%= new Date() %></h3> </div> <jsp:include page="include_footer.jsp" flush="false" /> </body> </html>
HTML
๋ณต์‚ฌ

include_header.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> <header> <h1>ํ—ค๋” ์˜์—ญ</h1> </header> </body> </html>
HTML
๋ณต์‚ฌ

include_footer.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> <footer> <p>ํ—ค๋” ์˜์—ญ</p> </footer> </body> </html>
HTML
๋ณต์‚ฌ

๋””๋ ‰ํ‹ฐ๋ธŒ include vs ์•ก์…˜ include

1.
๋””๋ ‰ํ‹ฐ๋ธŒ include
โ€ข
ํ˜•์‹ : <%@ include file="includedPage.jsp" %>
โ€ข
ํฌํ•จ ์‹œ๊ฐ„ : ์ปดํŒŒ์ผ ์‹œ๊ฐ„
โ—ฆ
์›๋ณธ JSP ํŽ˜์ด์ง€์™€ ํฌํ•จ๋˜๋Š” JSP ํŽ˜์ด์ง€๊ฐ€ ํ•˜๋‚˜์˜ ์†Œ์Šค ์ฝ”๋“œ๋กœ ํ•ฉ์ณ์ ธ์„œ ์ปดํŒŒ์ผ
โ€ข
์ฃผ์š” ์˜ˆ์‹œ : ํŽ˜์ด์ง€์˜ ๊ณตํ†ต๋œ ํ—ค๋”๋‚˜ ํ‘ธํ„ฐ
โ€ข
์‹คํ–‰ ์‹œ๊ฐ„์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋™์ ์œผ๋กœ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Œ
2.
์•ก์…˜ include
โ€ข
ํ˜•์‹ : <jsp:include page="includedPage.jsp" />
โ€ข
ํฌํ•จ ์‹œ๊ฐ„ : ๋Ÿฐํƒ€์ž„ ์‹œ๊ฐ„ํฌํ•จ
โ—ฆ
JSP ํŽ˜์ด์ง€๊ฐ€ ํ˜„์žฌ ํŽ˜์ด์ง€์™€ ๋ณ„๊ฐœ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰
โ€ข
์ฃผ์š” ์˜ˆ์‹œ : ๋™์  ์ฝ˜ํ…์ธ  ํฌํ•จ ํ•˜๋Š” ๊ฒฝ์šฐ (ํŽ˜์ด์ง€๋ณ„ ์„œ๋กœ ๋‹ค๋ฅธ ์„œ๋ธŒ ๋ฉ”๋‰ด ๋“ฑ)
โ€ข
์‹คํ–‰ ์‹œ๊ฐ„์— ํฌํ•จ๋˜๋ฏ€๋กœ ์š”์ฒญ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ˜ํ…์ธ ๋ฅผ ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ include ์‹ค์Šต์ฝ”๋“œ

์‹ค์ŠตํŒŒ์ผ

โ€ข
include_main.jsp
โ€ข
include_header.jsp
โ€ข
include_footer.jsp

include_main.jsp

<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ - include</title> </head> <body> <% String menu1 = "home"; String menu2 = "board"; String menu3 = "gallery"; %> <%-- ๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ include --%> <%@ include file="include_header.jsp" %> <div class="container"> <h1>์ปจํ…์ธ  ์˜์—ญ</h1> <h3><%=new Date()%></h3> </div> <% String copy = "copyright"; %> <%@ include file="include_footer.jsp" %> </body> </html>
HTML
๋ณต์‚ฌ

include_header.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> <header> <h1>ํ—ค๋” ์˜์—ญ</h1> <ul> <li><a href=""><%= menu1 %></a></li> <li><a href=""><%= menu2 %></a></li> <li><a href=""><%= menu3 %></a></li> </ul> </header> </body> </html>
HTML
๋ณต์‚ฌ

include_footer.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> <footer> <p>ํ‘ธํ„ฐ ์˜์—ญ</p> <p><%= copy %></p> </footer> </body> </html>
HTML
๋ณต์‚ฌ

๊ฒฐ๊ณผ ํ™”๋ฉด

์•ก์…˜ ํƒœ๊ทธ include ์‹ค์Šต์ฝ”๋“œ

์‹ค์ŠตํŒŒ์ผ

โ€ข
include_main.jsp
โ€ข
include_header.jsp
โ€ข
include_footer.jsp

include_main.jsp

<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>์•ก์…˜ํƒœ๊ทธ - include</title> </head> <body> <%-- ์•ก์…˜ํƒœ๊ทธ include --%> <jsp:include page="include_header.jsp" flush="false"> <jsp:param value="home" name="menu1"/> <jsp:param value="board" name="menu2"/> <jsp:param value="gallery" name="menu3"/> </jsp:include> <div class="container"> <h1>์ปจํ…์ธ  ์˜์—ญ</h1> <h3><%=new Date()%></h3> </div> <jsp:include page="include_footer.jsp" flush="false"> <jsp:param value="copyright" name="copy"/> </jsp:include> </body> </html>
HTML
๋ณต์‚ฌ

include_header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>์•ก์…˜ํƒœ๊ทธ - include</title> </head> <body> <% String menu1 = request.getParameter("menu1"); String menu2 = request.getParameter("menu2"); String menu3 = request.getParameter("menu3"); %> <header> <h1>ํ—ค๋” ์˜์—ญ</h1> <ul> <li><a href=""><%= menu1 %></a></li> <li><a href=""><%= menu2 %></a></li> <li><a href=""><%= menu3 %></a></li> </ul> </header> </body> </html>
HTML
๋ณต์‚ฌ

include_footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>์•ก์…˜ํƒœ๊ทธ - include</title> </head> <body> <footer> <p>ํ‘ธํ„ฐ ์˜์—ญ</p> <p><%= request.getParameter("copy") %></p> </footer> </body> </html>
HTML
๋ณต์‚ฌ

๊ฒฐ๊ณผ ํ™”๋ฉด

param ์•ก์…˜ํƒœ๊ทธ

: ํ˜„์žฌ JSP ํŽ˜์ด์ง€์—์„œ ๋‹ค๋ฅธ JSP ํŽ˜์ด์ง€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๊ธฐ ์œ„ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ํƒœ๊ทธ

์‹ค์Šต ํŒŒ์ผ

โ€ข
param.jsp
โ€ข
detail.jsp

param.jsp

<jsp:forward page="detail.jsp"> <jsp:param name="id" value="ALOHA" /> <jsp:param name="age" value="20" /> </jsp:forward>
HTML
๋ณต์‚ฌ

detail.jsp

์•„์ด๋”” : <%=request.getParameter("id")%> ๋‚˜์ด : <%=request.getParameter("age")%>
HTML
๋ณต์‚ฌ

์ž๋ฐ”๋นˆ์ฆˆ (JavaBeans)

: ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋Š” ์ž๋ฐ” ํด๋ž˜์Šค
โ€ข
์ž๋ฐ”๋นˆ์ฆˆ์˜ ์กฐ๊ฑด
โ—ฆ
Serializable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ (๊ฐ์ฒด ์ง๋ ฌํ™”)
โ—ฆ
๊ธฐ๋ณธ ์ƒ์„ฑ์ž
โ—ฆ
private ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
โ—ฆ
getter/setter ๋ฉ”์†Œ๋“œ
โ—ฆ
๋ฐ˜๋“œ์‹œ ํŒจํ‚ค์ง€ ๋‚ด์— ์žˆ์–ด์•ผํ•จ (default package ์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€)

์ž๋ฐ”๋นˆ์ฆˆ๋ฅผ ์œ„ํ•œ ์•ก์…˜ ํƒœ๊ทธ

โ€ข
useBean
<jsp:useBean id="์ž๋ฐ”๋นˆ์ฆˆ id(๊ฐ์ฒด๋ช…)" class="ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…" />
HTML
๋ณต์‚ฌ
โ€ข
getProperty
<jsp:getProperty property="์†์„ฑ๋ช…(๋ณ€์ˆ˜๋ช…)" name="์ž๋ฐ”๋นˆ์ฆˆ id(๊ฐ์ฒด๋ช…)"/>
HTML
๋ณต์‚ฌ
โ€ข
setProperty
<jsp:setProperty property="์†์„ฑ๋ช…(๋ณ€์ˆ˜๋ช…)" name="์ž๋ฐ”๋นˆ์ฆˆ id(๊ฐ์ฒด๋ช…)" value="์ง€์ •ํ•  ๊ฐ’"/>
HTML
๋ณต์‚ฌ

์‹ค์Šต ํŒŒ์ผ

โ€ข
Person.java
โ€ข
person01.jsp - ์Šคํฌ๋ฆฝํ‹€๋ฆฟ์œผ๋กœ getter/setter
โ€ข
person02.jsp - ์•ก์…˜ํƒœ๊ทธ๋กœ getter/setter

Person ์ž๋ฐ”๋นˆ์ฆˆ

public class Person implements Serializable { private static final long serialVersionUID = 1L; // private ๋ณ€์ˆ˜ private String name; private int age; // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž public Person() { this.name = "๊น€์กฐ์€"; this.age = 1; } // getter/setter public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Java
๋ณต์‚ฌ

person01.jsp

โ€ข
getter/setter
<%-- p1 ์ด๋ฆ„์œผ๋กœ Person ๊ฐ์ฒด ์ƒ์„ฑ --%> <jsp:useBean id="p1" class="beans.Person" /> <h3>์•„์ด๋”” : <%= p1.getName() %></h3> <h3>๋‚˜์ด : <%= p1.getAge() %></h3> <hr> <%-- p2 ์ด๋ฆ„์œผ๋กœ Person ๊ฐ์ฒด ์ƒ์„ฑ --%> <jsp:useBean id="p2" class="beans.Person" /> <% p2.setName("ํ™๊ธธ๋™"); p2.setAge(20); %> <h3>์•„์ด๋”” : <%= p2.getName() %></h3> <h3>๋‚˜์ด : <%= p2.getAge() %></h3>
Java
๋ณต์‚ฌ

person02.jsp

โ€ข
์•ก์…˜ ํƒœ๊ทธ get/set
<jsp:useBean id="person" class="beans.Person" /> <h3>์ด๋ฆ„ : <jsp:getProperty property="name" name="person"/></h3> <h3>๋‚˜์ด : <jsp:getProperty property="age" name="person"/></h3> <hr> <jsp:setProperty property="name" name="person" value="ํ™๊ธธ๋™"/> <jsp:setProperty property="age" name="person" value="20"/> <h3>์ด๋ฆ„ : <jsp:getProperty property="name" name="person"/></h3> <h3>๋‚˜์ด : <jsp:getProperty property="age" name="person"/></h3>
Java
๋ณต์‚ฌ