JiYoung Dev πŸ–₯

[JSP/Servlet] Scope (2023.05.18~19) λ³Έλ¬Έ

full stack/Back-End

[JSP/Servlet] Scope (2023.05.18~19)

Shinjio 2023. 5. 19. 09:38
Scope
(μ˜μ—­)

λ³€μˆ˜κ°€ μœ νš¨μ„±μ„ κ°–λŠ” μ˜μ—­!

 

 

πŸ“– Scope μ’…λ₯˜

 

 

βš™ Page Scope

 

 

- κ°’ μ €μž₯ : setAttribute

- κ°’ κ°€μ Έμ˜€κΈ° : getAttribute

 

 

μžλ°” λ³€μˆ˜μ™€ λ‹€λ₯Έμ  : EL ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•  수 μžˆλ‹€! 

 

<%@ 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 μ˜μ—­(scope)에 μ €μž₯된 값은 ν•΄λ‹Ή νŽ˜μ΄μ§€ μ•ˆμ—μ„œλ§Œ μ§€μ—­λ³€μˆ˜μ²˜λŸΌ μ‚¬μš© κ°€λŠ₯
	//page μ˜μ—­μ— μ €μž₯된 κ°’ >> el(express language)ν‘œκΈ°λ²•μœΌλ‘œ μ‚¬μš© κ°€λŠ₯
	//                     : μžλ°” λ³€μˆ˜λ‘œ μ„ μ–Έλœ 값은 μ‚¬μš© λΆˆκ°€λŠ₯
	//setAttribute(name(String),value(Object))
	pageContext.setAttribute("nickname", "ν’€μŠ€νƒ");
	String nick = (String)pageContext.getAttribute("nickname");
	%>
	
	<%=nick %><br>
	${nickname} <!-- page μ˜μ—­ μ•ˆμ— μ €μž₯된 nameκ°’ 지정! -->
	${nick} <!-- μžλ°” λ³€μˆ˜λŠ” μ‚¬μš© λΆˆκ°€λŠ₯ -->
	${pageScope.nickname } <!-- nickname이 μ„Έμ…˜μ—λ„ μ‚¬μš©λ˜λŠ” λ“±μ˜ 쀑볡 μ‚¬μš©λ˜λŠ” 경우 μ‚¬μš© -->

	<a href="Ex01page2.jsp">2둜 이동</a>

</body>
</html>

 

βš™ Request Scope

 

 

 

 

νŽ˜μ΄μ§€ μ˜μ—­μ•ˆμ— μ €μž₯된 값은 μ ˆλŒ€λ‘œ λ‹€λ₯Έ νŽ˜μ΄μ§€λ‘œ λ„˜κ²¨μ€„ 수 μ—†μŒ!

 

 

forwardλŠ” ν•œ 번만 λ‹€λ₯Έ νŽ˜μ΄μ§€μ— 정보λ₯Ό λ„˜κ²¨μ€„ 수 있음. 

이후 νŽ˜μ΄μ§€μ— 계속 데이터λ₯Ό λ„˜κ²¨μ£ΌκΈ° μœ„ν•΄μ„œλŠ” forwardλ₯Ό 계속 μ‚¬μš©ν•΄μ€˜μ•Ό 함. -> session을 μ‚¬μš©ν•˜λŠ” 이유. 

 

<%@ 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.setAttribute("nickname", "ν’€μŠ€νƒ");
		String nick = (String)request.getAttribute("nickname");
		
		//pageContext.setAttribute("nickname", "ν’€μŠ€νƒ22");
		//String nick1 = (String)pageContext.getAttribute("nickname");
	%>
	
	<%=nick %><br>
	${nickname } <!-- 2개 μ΄μƒμ˜ μ˜μ—­μ—μ„œ 같은 name 값을 μ‚¬μš©ν•˜λ©΄ 더 쒁은 μ˜μ—­μ˜ 값을 κ°€μ Έμ˜΄ -->
	${requestScope.nickname }
	
	<a href="Ex02request2.jsp">2둜 이동</a>
</body>
</html>

 

forward μ‚¬μš©λ²•

1) RequestDispatcher 객체 생성 ν›„ forward() λ©”μ„œλ“œ μ‚¬μš©

2) pageContext.forward()

3) <jsp: forward page="">

 

<%@ 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.setAttribute("nickname", "ν’€μŠ€νƒ");
	String nick = (String)request.getAttribute("nickname");
	
	//ν¬μ›Œλ”© 방식 이동
	//1. RequestDispathcer ν™œμš©
	//RequestDispatcher rd = request.getRequestDispatcher("Ex02request2.jsp");
	//rd.forward(request, response);
	//servlet, jsp λͺ¨λ‘ μ‚¬μš© κ°€λŠ₯
	
	//2. pageContext ν™œμš©(νŽ˜μ΄μ§€μ˜ 흐름 μ œμ–΄)
	//pageContext.forward("Ex02request2.jsp");
	//jsp μ‚¬μš© κ°€λŠ₯
	
	%>
	
	<!-- 3. μ•‘μ…˜νƒœκ·Έ ν™œμš© : JSPμ—μ„œ μ œκ³΅ν•˜κ³  μžˆλŠ” μ»€μŠ€ν…€ νƒœκ·Έ, 
			μ„œλ²„-ν΄λΌμ΄μ–ΈνŠΈ κ°„μ˜ μ–΄λ–€ 행동을 μ·¨ν•˜λ„λ‘ λͺ…λ Ήν•˜λŠ” νƒœκ·Έ 
			jspμ—μ„œλ§Œ μ‚¬μš© κ°€λŠ₯ -->
	<jsp:forward page="Ex02request2.jsp"/>
	
</body>
</html>

 

βš™ Session Scope

 

 

 

remove, invalidate, λΈŒλΌμš°μ € 끄기 μ „κΉŒμ§€ μ‚¬μš© κ°€λŠ₯함

 

<%@ 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("nickname", "ν’€μŠ€νƒ");
		String nick = (String)session.getAttribute("nickname");
	%>
	
	<%=nick %><br>
	${nickname }
	
	<a href="Ex03Session2.jsp">2둜 이동</a>
	
</body>
</html>

 

βš™ Application 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>
	<%
		application.setAttribute("nickname", "ν’€μŠ€νƒ");
		String nick = (String)application.getAttribute("nickname");
	%>
	
	<%=nick %><br>
	${nickname }
	${applicationScope.nickname }
	<a href="Ex04application2.jsp">2둜 이동</a>
</body>
</html>