웹개발 교육/jsp

[55일] jsp (23) - SCOPE

ewok 2022. 10. 17. 14:44
● [페이지 이동]
1) <a href="?"></a>
2) location.href=""
3) <form action=""></form>
4) <jsp:forward page=""></jsp:forward>
5) response.sendRedirect("")

 

02_scopeTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>02_scopeTest.jsp</title>
</head>
<body>
	<h3>웹페이지의 SCOPE(유효범위)</h3>
<%-- 
	● [페이지 이동]
	 1) <a href="?"></a>
	 2) location.href=""
	 3) <form action=""></form>
	 4) <jsp:forward page=""></jsp:forward>
	 5) response.sendRedirect("")
--%>
<%
	pageContext.setAttribute("one", 100);  //02_scopeTest.jsp 현재 페이지에서만 유효
	request.setAttribute("two", 200);
	session.setAttribute("three", 300);
	application.setAttribute("uid", "ITWILL");
%>
	<!-- 1) request.getAttribute("two) null값 -->
	<!-- 
	<a href="02_scopeResult.jsp">[SCOPE 결과 페이지 이동]</a>
	-->
	 
	<!-- 2) request.getAttribute("two) null값 -->
	<!--
	<script>
		alert("[SCOPE 결과 페이지 이동]");
		location.href="02_scopeResult.jsp";
	</script>
	-->
	
	<!-- 3) request.getAttribute("two) null값 -->
	<!-- 
	<form action="02_scopeResult.jsp">
		<input type="submit" value="[SCOPE 결과 페이지 이동]">
	</form>
	--> 
	
	<!-- 4) 액션태그 페이지 이동 -->
	<!-- request.getAttribute("two) 200 접근 가능
	     request 내부변수는 부모페이지(02_scopeTest.jsp)와 자식페이지(02_scopeResult.jsp)에서만 유효하다 -->
	<%-- 
	<jsp:forward page="02_scopeResult.jsp"></jsp:forward>
	--%>
	
<%
	//5) request.getAttribute("two) null값
	//response.sendRedirect("02_scopeResult.jsp");
	
	//6) request.getAttribute("two) 200 접근 가능
	String view = "02_scopeResult.jsp";
	RequestDispatcher rd = request.getRequestDispatcher(view);
	rd.forward(request, response);
	
	/*
		내부변수          02_scopeTest.jsp(부모)    02_scopeResult.jsp(자식)
		----------------------------------------------------------------
		pageContext             ○                          X
		request                 ○                       ○ 또는 X
		session                 ○                          ○
		application             ○                          ○
		
	*/
%>
	
</body>
</html>

 

02_scopeResult.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>02_scopeResult.jsp</title>
</head>
<body>
	<h3>웹페이지의 SCOPE(유효범위) 결과</h3>
<%
	out.print("1)pageContext 영역 : " + pageContext.getAttribute("one") + "<hr>");
	out.print("2)request 영역 : " + request.getAttribute("two") + "<hr>");
	out.print("3)session 영역 : " + session.getAttribute("three") + "<hr>");
	out.print("4)application 영역 : " + application.getAttribute("uid") + "<hr>");
%>
</body>
</html>

1) <a href="?"></a>
2) location.href=""
3) <form action=""></form>
4) <jsp:forward page=""></jsp:forward>

 

 

정리해보면

내부변수 02_scopeTest.jsp(부모)  02_scopeResult.jsp(자식)
pageContext O X
request O O 또는 X
session O O
application O O