Attribute

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 19์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
8/17
post-thumbnail

๐Ÿ Attribute

  • Parameter๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ form์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜€๋‹ค๋ฉด, Attribute๋Š” ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ์—์„œ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์ด๋‹ค
  • ๊ฐ Attribute๋Š” ์ข…๋ฅ˜๋ณ„๋กœ ์œ ํšจํ•œ ๋ฒ”์œ„(Scope)๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ์šฉ๋„์— ๋งž๊ฒŒ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค
  • ์ข…๋ฅ˜๋กœ๋Š” Application, session, request, page๊ฐ€ ์žˆ๋‹ค

๐Ÿคบ Page์˜ Attribute

  • ํ•ด๋‹น JSPํŽ˜์ด์ง€ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์–ดํŠธ๋ฆฌ๋ทฐํŠธ (์ง€์—ญ๋ณ€์ˆ˜๊ฐ™์€ ๋Š๋‚Œ)
  • page scope์— ๋“ฑ๋กํ•œ attribute๋Š” ๋‹ค๋ฅธ๊ณณ์œผ๋กœ forward๋งŒ ํ•ด๋„ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค (forward์šฉ์ด ์•„๋‹˜)

๐Ÿคบ Request์˜ Attribute

  • ํ•˜๋‚˜์˜ ์š”์ฒญ์ด ์‘๋‹ต๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์œ ์ง€๋˜๋Š” attribute
  • forward, include๊นŒ์ง€๋Š” ์œ ์ง€๋˜๊ณ  ์‘๋‹ต์ด ๋ฐœ์ƒํ•˜๋ฉด ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค
  • ์ฒซ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ์–ป์–ด๋‚ธ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฌผ์„ view ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์š”์ฒญ ๊ฐ์ฒด์˜ attribute์— ์‹ค์–ด ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค
  • ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Key-Value ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค

    ์‹ค์–ด๋†“์„ ๋•Œ : setAttribute("key", value)
    ๊บผ๋‚ผ ๋•Œ : getAttribute("key")

  • ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ์‹ค์–ด๋†“์€ ๊ฐ’์€ ๊บผ๋‚ผ ๋•Œ ํ•ด๋‹น ํƒ€์ž…์ด Object๋กœ ๋‚˜์˜จ๋‹ค(ํ•„์š”ํ•˜๋‹ค๋ฉด ๋‹ค์šด์บ์ŠคํŒ…ํ•˜์—ฌ ์›๋ž˜์˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค/ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊บผ๋‚ผ ๋•Œ ๋ฌด์กฐ๊ฑด ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค)

๐Ÿคบ Session์˜ Attribute

  • ํ•˜๋‚˜์˜ ์„ธ์…˜์ด ๋งŒ๋ฃŒ๋  ๋•Œ ๊นŒ์ง€ ์ˆ˜๋ช…์ด ์œ ์ง€๋˜๋Š” attribute
  • ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด๋‹น ์„œ๋ฒ„์— ์ฒ˜์Œ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์„ธ์…˜ID๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š”๋‹ค
  • ํ•ด๋‹น ์„ธ์…˜ID๋Š” ์œ ์ผํ•œ ๊ฐ’์ด๋ฉฐ ํด๋ผ์ด์–ธํŠธ ๋ณธ์ธ์ž„์„ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ์ˆ˜๋‹จ์ด๋‹ค
  • ์„ธ์…˜ID๋Š” ์‹œ๊ฐ„์ด ๋ชจ๋‘ ์ง€๋‚˜๊ฑฐ๋‚˜ ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•˜๋ฉด ๋งŒ๋ฃŒ๋œ๋‹ค(ex: ์€ํ–‰)
  • ์„ธ์…˜์ด ์œ ์ง€๋˜๋Š” ์‹œ๊ฐ„์€ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ณ„์† ์œ ์ง€๋˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค

๐Ÿคบ Application์˜ Attribute

  • WAS(์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„)์— ์ €์žฅํ•ด ๋†“๋Š” attribute
  • ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€ ๊ณ„์† ์œ ์ง€๋œ๋‹ค
  • ์„ธ์…˜ ๊ฐ์ฒด๋Š” ์ ‘์†์ž ์ˆ˜ ๋งŒํผ ์กด์žฌํ•˜์ง€๋งŒ, ์„œ๋ฒ„๋Š” 1๋Œ€์ด๋ฏ€๋กœ ์ „์—ญ๋ณ€์ˆ˜๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค

๐Ÿ’ฌ ์ฝ”๋“œ >>
โœ” index.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Attribute Scope</title>
</head>
<body>
	<button id="test-btn">์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ถ”๊ฐ€ ํ›„ ํฌ์›Œ๋“œ ํ•ด๋ณด๊ธฐ... </button>
	<script src="/chap04/javascript/index.js"></script>
</body>
</html>

โœ” process.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	// html ๋ถ€๋ถ„์„ ๋‹ค ์ง€์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋ฉด jsp๋ฅผ ์„œ๋ธ”๋ฆฟ์ฒ˜๋Ÿผ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค (๋ถˆํŽธํ•จ) 
	
	// page scope: ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ๋งŒ๋ฃŒ, ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์ง€์—ญ๋ณ€์ˆ˜
	pageContext.setAttribute("flavor", "strawberry");
	// request scope : ์‘๋‹ต์„ ํ•˜๊ณ ๋‚˜๋ฉด ๋งŒ๋ฃŒ, ํ•˜๋‚˜์˜ ์š”์ฒญ์ด ์‘๋‹ต๋˜๊ธฐ ์ „(์ฒ˜๋ฆฌ๋˜๊ธฐ ์ „)๊นŒ์ง€ ์œ ์ง€
	request.setAttribute("flavor", "kiwi");
	// session scope : ์„ธ์…˜์ด ๋งŒ๋ฃŒ๋˜๋ฉด ํ•จ๊ป˜ ์‚ฌ๋ผ์ง„๋‹ค, ํ˜„์žฌ ์ ‘์†์ž ๋งŒํผ ์กด์žฌ
	session.setAttribute("flavor", "orange");
	// application scope : ์„œ๋ฒ„๊ฐ€ ์ผœ์ ธ ์žˆ๋Š” ๋™์•ˆ ๊ณ„์† ์œ ์ง€๋œ๋‹ค 
	application.setAttribute("flavor", "banana");

	// getRequestDispatcher ์ƒ์„ฑ์‹œ ํ•„์š”ํ•œ ๊ฒฝ๋กœ๋Š” webapp ๋ฐ‘์˜ ๊ฒฝ๋กœ
	request.getRequestDispatcher("/attribute_scope/view.jsp")
	.forward(request, response);

%>

โœ” view.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>๊ฐ ์˜์—ญ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊บผ๋‚ด์„œ ํ™•์ธํ•ด๋ณด๊ธฐ</title>
</head>
<body>
	<h3># attribute ํ™•์ธ</h3>
	<ul>
		<li>page : <%= pageContext.getAttribute("flavor")%></li>
		<li>request :<%= request.getAttribute("flavor")%></li>
		<li>session : <%= session.getAttribute("flavor")%></li>
		<li>application : <%= application.getAttribute("flavor")%></li>
	</ul>
</body>
</html>

๐Ÿ˜Ž ์ „์ฒด์  ํ๋ฆ„ >>

  • req1 ๊ณผ req3์˜ ๋‚ด์šฉ์€ ๊ฐ™์„ ์ˆ˜ ์žˆ๋‹ค(setAttribute๋ฅผ ํ†ตํ•ด์„œ ์š”์ฒญ์„ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ)
  • req2๋Š” null๊ฐ’์ด ์ „์†ก๋œ๋‹ค (๋ฐ›์€ ์š”์ฒญ์ด ์—†์Œ)
  • request์˜ ๊ฒฝ์šฐ view.jsp๊นŒ์ง€ ์ „๋‹ฌ๋˜๋ฉฐ view.jsp์— ๋„๋‹ฌํ•˜์—ฌ response๋ฅผ ๋ฐ›์„ ๊ฒฝ์šฐ ์‚ฌ๋ผ์ง„๋‹ค

๐Ÿ“บ ํ™”๋ฉด ์ถœ๋ ฅ >>

โœ” index.jsp์—์„œ ์ ‘์†ํ–ˆ์„ ๊ฒฝ์šฐ(req1, req3 ๋™์ผ) >>

โœ” view.jsp์—์„œ ์ ‘์†ํ–ˆ์„ ๊ฒฝ์šฐ(req2) >>

profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€