영역 객체 (Scope)와 속성(Attribute)

JSP에서 제공하는 내장 객체 중 session, request, application 객체들은 해당 객체에 정의된 유효 범위 안에서 필요한 데이터를 저장하고 읽어오며 서로 공유할 수 있는 특정한 영역이 있다.

 

공유되는 데이터를 (Attribute)속성

속성을 공유하는 유효 범위를 (Scope)영역

 

session 내장 객체는 세션이 유지되고 있는 동안에 값이 유효하고 세션이 종료 되는 순간에 반환된다.(버림)

 

request 객체는 클라이언트 요청이 처리되는 동안에 데이터를 사용할 수 있고, 

 

application 객체는 해당 웹 어플리케이션이 실행되는동안 유효하다.

 

영역 영역 객체 속성의 유효 범위
page pageContext 해당 페이지가 클라이언트에 서비스를 제공하는 동안에만 유효(서블릿 인스턴스 _jspService() 메소드가 실행되는 동안에만 유효)
request request 클라이언트의 요청이 처리되는 동안 유효
session session 세션이 유지되는 동안 유효(하나의 브라우저(클라이언트)에 1개의 세션이 생성
application application 웹 어플리케이션이 실행되고 있는 동안 유효

 

리턴 메소드명 해설
Object getAttribute(String key) key 값으로 등록되어 있는 속성을 Object 타입으로 리턴 (key 값에 해당하는 속성이 없을 경우 null을 리턴)
Enumeration getAttributeNames() 해당 영역에 등록되어 있는 모든 속성들의 이름을 Enumeration 타입으로 리턴
없음 setAttribute(String key, Object obj) 해당 영역에 key 값의 이름으로 obj 객체를 등록
없음 removeAttribute(String key) key 값으로 등록되어 있는 속성을 제거

예제

<attributeTest1_Form.jsp>

<%@ 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>
<h2>영역과 속성 테스트</h2>
<form action = "attributeTest1.jsp" method ="post">
<table border = "1">
	<tr>
		<td colspan = "2"> Application 영역에 저장할 내용들 </td>
	</tr>
	
	<tr>
		<td>이름 </td>
		<td><input type = "text" name = "name"></td>
	</tr>
	
	<tr>
		<td>아이디</td>
		<td><input type = "text" name = "id"></td>
	</tr>
	
	<tr>
		<td colsapn = "2"> <input type = "submit" value ="전송"></td>
	</tr>
	
</table>
</form>
</body>
</html>

<attributeTest1.jsp>

<%@ 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>
	<h2>영역과  속성 테스트</h2>
	<%
	request.setCharacterEncoding("UTF-8");
	String name = request.getParameter("name");
	String id = request.getParameter("id");
	
	if(name!=null && id!= null)
	{
	    application.setAttribute("name", name);
	    application.setAttribute("id", id);
	}
	%>
	<h3><%=name %>님 반갑습니다.<br> <%=name %> 님의 아이디는 <%=id %> 입니다</h3>
	<form action = "attributeTest2.jsp" method = "post">
		<table border = "1">
			<tr>
				<td colsapn = "2"> Session 영역에 저장할 내용들 </td>
			</tr>
			
			<tr>
				<td>
					e-mail 주소
				</td>
				<td>
					<input type = "text" name = "email">
				</td>
			</tr>
			
			<tr>
				<td>집 주소</td>
				<td><input type = "text" name = "address">
			</tr>
			
			<tr>
				<td> 전화번호</td>
				<td> <input type = "text" name = "tel"> 
			</tr>	
		
			<tr>
				<td colspan = "2"><input type ="submit" value = "전송"></td>
			</tr>
		</table>
	</form>
</body>
</html>

<attributeTest2.jsp>

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Attribute Test</title>
</head>
<body>
<h2>영역과 속성 테스트</h2>
<%
request.setCharacterEncoding("UTF-8");
String email = request.getParameter("email");
String address = request.getParameter("address");
String tel = request.getParameter("tel");

session.setAttribute("email", email);
session.setAttribute("address", address);
session.setAttribute("tel", tel);

String name = (String)application.getAttribute("name");
%>

<h3><%=name %> 님의 정보가 모두 저장되었습니다.</h3>
<a href = "attributeTest3.jsp"> 확인하러 가기 </a>
</body>
</html>

<attributeTest3.jsp>

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%@ page import = "java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Attribute Test</title>
</head>
<body>
	<h2>영역과 속성 테스트</h2>
	<table border = "1">
		<tr>
			<td colspan="2">Application 영역에 저장된 내용들</td>
		</tr>

		<tr>
			<td>이름</td>
			<td><%=application.getAttribute("name")%></td>
		</tr>

		<tr>
			<td>아이디</td>
			<td><%=application.getAttribute("id")%></td>
		</tr>
	</table>
	<br>
	<table border ="1">>
		<tr>
			<td colspan="2">Session 영역에 저장된 내용들 </td>
		</tr>
	<%
	Enumeration e = session.getAttributeNames();
	while(e.hasMoreElements())
	{
	    String attributeName = (String)e.nextElement();
	    String attributeValue = (String)session.getAttribute(attributeName);
	    %>
	    <tr>
	    	<td>
	    		<%=attributeName %>
	    	</td>
	    	
	    	<td>
	    		<%=attributeValue %>
	   		</td>
	   	</tr>
	   	<% 	
	}
	%>	
	</table>
</body>
</html>

 

 

 

 

 

'JSP' 카테고리의 다른 글

[JSP]포워딩  (0) 2019.11.04
[JSP] session scope 와 application scope 차이  (0) 2019.11.04
[JSP] config - 객체  (0) 2019.11.02
[JSP] out - 객체  (0) 2019.11.02
[JSP] application - 객체  (0) 2019.11.02