[SCOPE์ ์ ์]
์น ์๋ฒ์์ ๊ฐ์ฒด ๋๋ ๋ณ์๊ฐ ์์ฑ๋ํย ์ ํจย ํ ์ย ์๋ ๋ฒ์๋ฅผ ๋งํ๋ค.
์ด 4๊ฐ์ง ์ข ๋ฅ์ scope๊ฐ ์กด์ฌ ํ๋ค.
Page : ํ์ด์ง ๋ด์์ ์ง์ญ๋ณ์์ฒ๋ผ ์ฌ์ฉ
Request :ย http์์ฒญ์ WAS๊ฐ ๋ฐ์์ ์น ๋ธ๋ผ์ฐ์ ์๊ฒ ์๋ตํ ๋๊น์ง ๋ณ์๊ฐ ์ ์ง๋๋ ๊ฒฝ์ฐ ์ฌ์ฉ
Session :ย ์น ๋ธ๋ผ์ฐ์ ๋ณ๋ก ๋ณ์๊ฐ ๊ด๋ฆฌ๋๋ ๊ฒฝ์ฐ ์ฌ์ฉ
Application :ย ์น ์ดํ๋ฆฌ์ผ์ด์ ์ด ์์๋๊ณ ์ข ๋ฃ๋ ๋๊น์ง ๋ณ์๊ฐ ์ ์ง๋๋ ๊ฒฝ์ฐ ์ฌ์ฉย (๋ชจ๋ ํด๋ผ์ด์ธํธ์๊ฒ๊ณตํต์ ์ผ๋ก ๋ฌด์ธ๊ฐ๋ฅผ ๋ณด์ฌ ์ค๋ ์ฌ์ฉ)
Scope ์ข ๋ฅ | ์ ์ง ๋ฒ์ | ํ์ฉ ๋ฐฉ๋ฒ | JSP ๋ด์ฅ ๊ฐ์ฒด | servlet ๊ฐ์ฒด |
---|---|---|---|---|
Page Scope | ์์ฒญ๋ pageย | |||
๋ด๋ถ์์์ฌ์ฉ | ์ง์ญ ๋ณ์ ์ฒ๋ผ ํ์ฉ JSP์์ ์ฌ์ฉ | pageContext | ||
Request Scope | ์์ฒญ ๋ถํฐ ์๋ต๊น์ง | Forward๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋๊ธธ๋ | request | HttpServletRequest |
Session Scope | Web Browser ์ข ๋ฃ ๋๋ย | |||
์ค์ ํ ์๊ฐ ๋์ | ์ฌ์ฉ์ ๋ณ๋ก ์ ๋ณด๋ฅผ ์ ์ฅํ ๋ย | |||
(Ex:์ฅ๋ฐ๊ตฌ๋) | session | HttpServletRequest์ getSession()๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ session ๊ฐ์ฒด๋ฅผ ์ป๋๋ค | ||
HttpSession session = request.getSession(false); | ||||
Application Scope | Web Application์ด ์ข ๋ฃ ๋ ๋ ๊น์ง | ์ฌ์ฉ์์๊ฒ ๊ณตํต์ ์ผ๋ก ๋ฌด์๊ฐ๋ | ||
๋ณด์ฌ ์ฃผ๊ณ ์ ํ ๋ | application | getServletContext(); ๋ก ์ป๋๋ค | ||
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>
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>
<%@ 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>
<%@ 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>