1. 네 가지 Scope
- Application: 웹 애플리케이션이 시작되고 종료될 때까지 유지되는 경우
- Session: 웹 브라우저 별로 변수가 관리되는 경우
- Request: 하나의 HTTP 요청을 WAS가 받아서 웹 브라우저에게 응답할 때까지 변수가 유지되는 경우
- Page: 페이지 내에서 지역 변수처럼 사용
2. Page scope
- PageContext 추상 클래스를 사용
- JSP 페이지에서 pageContext라는 내장 객체로 사용 가능
- forward 될 경우 해당 Page scope에 지정된 변수는 사용할 수 없음 (당연. 페이지 범위 내에서 유효하기에)
- 마치 지역 변수처럼 사용됨
- JSP에서 pageScrope에 값을 저장한 후, 해당 값을 EL 표기법 등에서 사용할 때 사용됨
3. Request scope
- HTTP 요청을 WAS가 받아서, 웹 브라우저에게 응답할 때까지 변수 값을 유지하고자 하는 경우 사용
- HttpServletRequest 객체를 사용
- JSP에서는 request 내장 변수를 사용
- Servlet에서는 HttpServletRequest 객체를 사용
- 값을 저장할 때는 request 객체의 setAttribute() 메소드를 사용
- 값을 읽어들일 때는 request 객체의 getAttribute() 메소드를 사용
- Foward 시 값을 유지하고자 할 때 사용
4. Session scope
- 웹 브라우저(클라이언트)별로 변수를 관리하고자 할 경우 사용
- 웹 브라우저의 탭 간에는 세션 정보가 공유되기에, 각각의 탭에서는 같은 세션 정보를 사용할 수 있음
- HttpSession 인터페이스를 상속 받은 객체를 사용
- JSP에서는 session 내장 변수를 사용
- Servlet에서는 HttpServletRequest의 getSession() 메소드를 이용하여 session 객체를 얻음
- session 객체의 setAttribute(), getAttribute() 메소드를 사용
- 장바구니처럼 사용자별로 유지가 되어야 할 정보가 있을 때 사용
5. Application scope
- 웹 애플리케이션이 시작되고 종료될 때까지 변수를 사용
- ServletContext 인터페이스를 상속 받은 객체를 사용
- JSP에서는 application 내장 객체를 사용
- Servlet에서는 getServletContext() 메소드를 이용하여 application 객체를 이용
- 웹 애플리케이션당 하나의 application 객체가 사용됨
- application 객체의 setAttribute(), getAttribute 메소드를 사용
- 모든 클라이언트가 공통으로 사용해야 할 값들이 있을 때 사용
# ApplicationScope01.java
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ApplicationScope01")
public class ApplicationScope01 extends HttpServlet {
public ApplicationScope01() {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext servletContext = getServletContext();
int value = 1;
servletContext.setAttribute("value", value);
out.println("<h1>value: " + value + "</h1><br>");
}
}
# ApplicationScope02.java
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ApplicationScope02")
public class ApplicationScope02 extends HttpServlet {
public ApplicationScope02() {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext servletContext = getServletContext();
try {
int value = (int) servletContext.getAttribute("value");
value++;
servletContext.setAttribute("value", value);
out.println("<h1>value: " + value + "</h1><br>");
} catch (NullPointerException e) {
out.print("value의 값이 설정되지 않았음<br>");
}
}
}
# applicationScope01.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>application scope 실습</title>
</head>
<body>
<%
try {
int value = (int)application.getAttribute("value");
value += 2;
application.setAttribute("value", value);
%>
<h1><%=value%></h1>
<%
} catch (NullPointerException e) {
%>
<h1>설정된 값이 없습니다.</h1>
<%
}
%>
</body>
</html>
'강의 노트 > 웹 프로그래밍(풀스택)' 카테고리의 다른 글
[boostcourse] 2.11. Web API - BE (0) | 2023.06.21 |
---|---|
[boostcourse] 2.10. JDBC - BE (0) | 2023.06.20 |
[boostcourse] 2.4. Redirect & Forward - BE (0) | 2023.06.20 |
[boostcourse] 2.3. JSP - BE (0) | 2023.06.20 |
[boostcourse] 2.2. WEB UI 개발 - FE (0) | 2023.06.19 |