웹 개발 기초/자바 웹을 다루는 기술

application 내장 객체에 데이터 바인딩 실습

sungw00 2023. 2. 28. 09:43
728x90

1. appTest1.jsp, appTest2.jsp 파일 작성

<%-- appTest1.jsp --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	<%-- 이름과 주소를 session과 application 내장 객체에 바인딩 함 --%>
	session.setAttribute("name", "이순신");
	application.setAttribute("address", "서울시 성동구");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>내장 객체 스코프 테스트1</title>
</head>
<body>
	<h1>이름과 주소를 저장합니다.</h1>
	<a href=appTest2.jsp>두 번째 웹 사이트로 이동</a>
</body>
</html>
<%-- appTest2.jsp --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	<%-- 첫 번째 웹 페이지에서 저장한 데이터를 session과 application 내장 객체에서 가져옴 --%>
	String name = (String)session.getAttribute("name");
	String address = (String)application.getAttribute("address");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>이름은 <%= name %>입니다.</h1>
	<h1>주소는 <%= address %>입니다.</h1>
</body>
</html>

 

2. http://localhost:8090/pro12/test01/appTest1.jsp로 요청

첫 번째 JSP에서 name과 address를 session과 application에 바인딩

http://localhost:8090/pro12/test01/appTest1.jsp 요청 결과

 

3. 같은 브라우저에서 요청할 경우 두 번째 JSP에서 session과 application에 접근할 수 있음

두 번째 웹 사이트로 이동한 결과

 

하지만 엣지 브라우저에서는 application의 값에만 접근할 수 있음

엣지에서 http://localhost:8090/pro12/test01/appTest2.jsp를 요청한 결과

 

같은 브라우저에서 appTest2.jsp를 요청하면 session과 application에 저장된 값을 그대로 출력하지만, 다른 브라우저로 요청할 경우 session 내장 객체의 스코프는 접근할 수 없으므로 null을 출력한다. 이를 통해 application 내장 객체의 스코프는 애플리케이션 전체이고, session 내장 객체의 스코프는 같은 브라우저임을 알 수 있다.

 

728x90