[JSP study]영역 객체와 속성

Noah97·2022년 5월 17일
0

JspStudy

목록 보기
5/18
post-thumbnail

📂 JSP의 영역

  • JSP에서 정의하는 영역은 page, request, session, application으로 구성되어 있으며 이들 영역은 각각 pageContext, request,session, application 내장 객체를 통해서 속성을 설정하거나 읽어 들일 수 있다.

영역 객체

영역영역 객체속성의 유효 범위
pagepageContext해당 페이지가 클라이언트에 서비스를 제공하는 동안에만 유효
(서블릿 인스턴스의 _jspServicer()메소드가 실행되는 동안에만 유효)
requestrequest클라이언트의 요청이 처리되는 동안 유효
(포워딩 또는 include를 이용하는 경우 여러 개의 페이지에서도 요청 정보가 계속 유지되므로
request영역의 속성을 여러 페이지에서 공유할 수 있다.)
sessionsession세션이 유지되는 동안 유효
(하나의 브라우저(클라이언트)에 1개의 세션이 생성되므로 같은 웹브라우저 내에서 실행되는
페이지들이 속성을 공유할 수 있다.)
applicationapplication웹 애플리케이션이 실행되고 있는 동안 유효
(웹 컨테이너에서 해당 애플리케이션은 오직 하나만이 실행되므로 4가지 영역 중 가장 큰
영역에 해당한다.)
웹 컨테이너를 종료하면 애플리케이션 영역 객체가 소멸된다.

attribute ex)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Attribute Test Form</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>
		<td colspan="2"><input type = "submit" value="전송"></td>
	</tr>
</table>
</form>
</body>
</html>

<%@ 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 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 colspan="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"></td>
		</tr>
		<tr>
			<td>전화번호</td>
			<td><input type="text" name="tel"></td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="전송"></td>
	</table>
	</form>
</body>
</html>

<%@ 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>

<%@ 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 attribuValue = (String)session.getAttribute(attributeName);
	%>
	<tr>
		<td><%=attributeName %></td>
		<td><%=attribuValue %></td>
	</tr>
<% } %>
</table>
</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>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>
pageValue = <%=pageContext.getAttribute("pageScope") %><br>
requestValue = <%=request.getAttribute("requestScope") %>
</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>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>
<jsp:forward page="attributeTest5Result.jsp"></jsp:forward>
</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>
pageValue = <%=pageContext.getAttribute("pageScope") %> <br>
requestValue = <%=request.getAttribute("requestScope") %>
</body>
</html>

profile
안녕하세요 반갑습니다😊

0개의 댓글