본문 바로가기
✨ Back-end/JSP

[JSP] 기본객체(Page, Request, Session, Application), 서블릿 스코프(Scope)

by 환풍 2023. 8. 9.
728x90

 

 

PAGE 영역

하나의 JSP 페이지를 처리할 때 사용되는 영역

한번의 요청을 처리.

하나의 JSP 페이지 내에서 공유될 값을 저장한다.

페이지 내에서 지역변수처럼 사용된다.

 

REQUEST 영역

하나의 HTTP 요청을 처리할 때 사용되는 영역

한번의 요청을 처리하는 데 사용되는 모든 JSP 페이지에서 공유될 값을 저장한다.

http 요청을 WAS가 받아 웹 브라우저에게 응답할 때까지 변수가 유지되는 경우 사용된다.

SESSION 영역

하나의 웹 브라우저와 관련된 영역

한 사용자와 관련된 정보를 JSP들이 공유하기 위해 사용된다.

웹 브라우저 별로 변수가 관리되는 경우 사용된다.

APPLICATION 영역

하나의 웹 어플리케이션과 관련된 영역

모든 사용자와 관련해서 공유할 정보를 저장한다.

웹 어플리케이션이 시작되고 종료될 때까지 변수가 유지되는 경우 사용된다.

 

setServlet 서블릿

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.controller;
 
import java.io.IOException;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
@WebServlet("/setServlet")
public class setServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    public setServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected  void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        //1.request 스코프에 저장, HttpServletRequest 이용.
        request.setAttribute("request""리퀘스트 스코프 테스트");
        
        // 2. Session 스코프 저장, HttpSession API 이용
        HttpSession session = request.getSession();
        session.setAttribute("session""세션 스코프 테스트");
        
        // 3. application 스코프에 저장, ServletContext API 이용
        ServletContext application = getServletContext();
        application.setAttribute("application""어플리케이션 스코프 테스트");
                
        
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
cs

 

getServlet 서블릿

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.controller;
 
import java.io.IOException;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
@WebServlet("/getServlet")
public class getServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public getServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // 1. request 스코프 데이터 얻기, HttpServletRequest API 사용,  단 getAttribute는 Object 리턴이므로 String 형변환 해줘야한다, life-cycle이 요청 - 응답에 해당
        String name = (String) request.getAttribute("request");
        System.out.println(name);
        
        // 2. Session 스코프 저장, HttpSession API 이용, life-cycle이 브라우저에 해당됨.
        HttpSession session = request.getSession();
        String sessionName = (String) session.getAttribute("session");
        System.out.println(sessionName);
        
        // 3. application 스코프에 저장, ServletContext API 이용
        ServletContext application = getServletContext();
        String appRequest = (String)application.getAttribute("application");
        System.out.println(appRequest);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
cs

 

setServlet 을 getServlet으로 페이지 이동시 request로 받은 데이터는 응답을 보낼 시 소멸하기 때문에 null이 된다.

같은 브라우저 내에서 데이터를 저장하고 서블릿을 옮겨다니면 session과 application 모두 데이터를 저장한다.

 

다른 브라우저에서 서블릿 이동시 session도 null이 뜰 것이며, application만 데이터가 저장된 것을 확인할 수 있다.

 

즉, request는 서블릿 소멸시 객체가 소멸되지만, session과 applicationContext는 서블릿이 소멸해도 남아있다.
반응형

댓글