JSP_Scope ๐Ÿ…๐Ÿฅญ

ํ™”์ดํ‹ฐ ยท2023๋…„ 12์›” 22์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
11/12




[SCOPE์˜ ์ •์˜]

  • ์›น ์„œ๋ฒ„์—์„œ ๊ฐ์ฒด ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋œํ›„ย ์œ ํšจย ํ•  ์ˆ˜ย ์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ๋งํ•œ๋‹ค.

  • ์ด 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ scope๊ฐ€ ์กด์žฌ ํ•œ๋‹ค.

  • Page : ํŽ˜์ด์ง€ ๋‚ด์—์„œ ์ง€์—ญ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ

  • Request :ย http์š”์ฒญ์„ WAS๊ฐ€ ๋ฐ›์•„์„œ ์›น ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์‘๋‹ตํ•  ๋•Œ๊นŒ์ง€ ๋ณ€์ˆ˜๊ฐ€ ์œ ์ง€๋˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ

  • Session :ย ์›น ๋ธŒ๋ผ์šฐ์ € ๋ณ„๋กœ ๋ณ€์ˆ˜๊ฐ€ ๊ด€๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ

  • Application :ย ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๊ณ  ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋ณ€์ˆ˜๊ฐ€ ์œ ์ง€๋˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉย (๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ๊ณตํ†ต์ ์œผ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ณด์—ฌ ์ค„๋•Œ ์‚ฌ์šฉ)

Scope ์ข…๋ฅ˜์œ ์ง€ ๋ฒ”์œ„ํ™œ์šฉ ๋ฐฉ๋ฒ•JSP ๋‚ด์žฅ ๊ฐ์ฒดservlet ๊ฐ์ฒด
Page Scope์š”์ฒญ๋œ pageย 
๋‚ด๋ถ€์—์„œ์‚ฌ์šฉ์ง€์—ญ ๋ณ€์ˆ˜ ์ฒ˜๋Ÿผ ํ™œ์šฉ JSP์—์„œ ์‚ฌ์šฉpageContext
Request Scope์š”์ฒญ ๋ถ€ํ„ฐ ์‘๋‹ต๊นŒ์ง€Forward๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธธ๋•ŒrequestHttpServletRequest
Session ScopeWeb Browser ์ข…๋ฃŒ ๋˜๋Š”ย 
์„ค์ • ํ•œ ์‹œ๊ฐ„ ๋™์•ˆ์‚ฌ์šฉ์ž ๋ณ„๋กœ ์ •๋ณด๋ฅผ ์ €์žฅํ• ๋•Œย 
(Ex:์žฅ๋ฐ”๊ตฌ๋‹ˆ)sessionHttpServletRequest์˜ getSession()๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ session ๊ฐ์ฒด๋ฅผ ์–ป๋Š”๋‹ค
HttpSession session = request.getSession(false);
Application ScopeWeb Application์ด ์ข…๋ฃŒ ๋ ๋•Œ ๊นŒ์ง€์‚ฌ์šฉ์ž์—๊ฒŒ ๊ณตํ†ต์ ์œผ๋กœ ๋ฌด์—‡๊ฐ€๋Š”
๋ณด์—ฌ ์ฃผ๊ณ ์ž ํ• ๋•ŒapplicationgetServletContext(); ๋กœ ์–ป๋Š”๋‹ค
ServletContext application = getServletContext();

[SCOPE์˜ ๊ณตํ†ต ํ•จ์ˆ˜]


<%@ 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>
<%
//page ์˜์–ต์—์„œ ๊ฐ’ ์ €์žฅ
//pageContext
pageContext.setAttribute("nick","ํŽ˜์ด์ง€ ์˜์—ญ");

//page ์˜์—ญ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
String nick = (String)pageContext.getAttribute("nick");
%>
 nickname : <%=nick %>
 <br>
 nickname (EL): ${nick }
 <br>
 <a href = "./ex01_pageScope2.jsp">2 ํŽ˜์ด์ง€๋กœ ์ด๋™</a>
</body>
</html>
<%@ 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>
<%
String nick = (String)pageContext.getAttribute("nick");

%>
nickname : <%=nick %>

</body>
</html>

Untitled

scope: ์˜์—ญ

<%@ 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>
<%
// request ์˜์—ญ ๊ฐ’ ์ €์žฅํ•˜๊ธฐ
request.setAttribute("nick","thithi");
//request ์˜์—ญ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
String nick = (String)request.getAttribute("nick");
//forward๋ฐฉ์‹์œผ๋กœ ํŽ˜์ด์ง€ ์ด๋™
RequestDispatcher rd = request.getRequestDispatcher("ex02_requestScope2.jsp");
rd.forward(request,response);
%>
<!--  ํ™”๋ฉด์— ์ถœ๋ ฅ + EL ๋ฌธ๋ฒ•์œผ๋กœ๋„ ์ถœ๋ ฅ -->

<a href = "ex02_requestScope2.jsp">2ํŽ˜์ด์ง€ ์ด๋™</a><br>
nickname : ${nick}<br>
nickname EL : <%=nick %>

</body>
</html>
<%@ 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>
<% String nick = (String)request.getAttribute("nick"); %>
nickname :${requestScope.nick}<br>
</body>
</html>

Untitled

<%@ 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>
<%

session.setAttribute("nick","thi");

//page ์˜์—ญ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
String nick = (String)session.getAttribute("nick");
%>
 nickname : <%=nick %>
 <br>
 nickname (EL): ${sessionScope.nick }
 <br>
 <a href = "./ex03_sessionScope2.jsp">2 ํŽ˜์ด์ง€๋กœ ์ด๋™</a>
</body>
</html>
<%@ 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>
<%
String nick = (String)session.getAttribute("nick");%>
nickname : ${sessionScope.nick}
</body>
</html>

Untitled

Untitled

Untitled

<%@ 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>
<%

application.setAttribute("nick","ํŽ˜์ด์ง€ ์˜์—ญ");

//page ์˜์—ญ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
String nick = (String)application.getAttribute("nick");
%>
 nickname : <%=nick %>
 <br>
 nickname (EL): ${sessionScope.nick }
 <br>
 <a href = "./ex04_applicationScope2.jsp">2 ํŽ˜์ด์ง€๋กœ ์ด๋™</a>
</body>
</html>
<%@ 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>
<%
//String nick = (String)application.getAttribute("nick");%>
nickname : ${sessionScope.nick}
</body>
</html>
profile
์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•ฉ์‹œ๋‹ค! The best is yet to come! ๐Ÿ’œ

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