티스토리 뷰

Scope ( 범위, lifecycle )

 

1️⃣ 개요

  • 임의의 데이터를 특정 객체에 저장할 수 있다.
  • 객체가 3가지 형태가 있고 lifecycle가 다르다. ★★

 


 

 

2️⃣ 종류 3가지

 

✅ HttpServletRequest ( request scope 라고 부른다. )
// Attributes are reset between requests
// request.getParameter(key) 구별해야 된다.
-저장방법: request.setAttribute(String key, Object value);
-조회: request.getAttribute(key);     형변환 필요
-삭제: request.removeAttribute(key);

 

 

✅ HttpSession ( session scope 라고 부른다. )

  HttpSession session = request.getSession();
   -저장방법:   session.setAttribute(String key, Object value);
   -조회:     session.getAttribute(key); 형변환 필요
   -삭제:     session.removeAttribute(key);

 

 

ServletContext ( application scope 라고 부른다. )

    ServletContext ctx = getServletContext();
   -저장방법:   ctx.setAttribute(String key, Object value);
   -조회:     ctx.getAttribute(key); 형변환 필요
   -삭제:     ctx.removeAttribute(key);

 

 


 

 

3️⃣ 생성·소멸 시기와 용도

 

 ServletContext

  • Web Application 관련
  • 생성: Web Application이 생성될 때 ( tomcat이 서비스할 준비가 될 때)
    ~
    소멸: Web Application이 종료될 때 ( tomcat 종료시, 또는 module 제거시 )
  • 용도: Web Application이 종료되기 전까지 계속적으로 사용하는 데이터 필요시

 

 

 HttpSession

  • 웹 브라우저와 관련
  • 생성: 웹 브라우저 열고 요청할 때
    ~
    소멸: 웹 브라우저 close 될 때 ( tomcat 기준으로 실제로는 30분 지나면 제거됨. 보안이슈 )
  • 용도: 브라우저가 종료되기 전까지 계속적으로 사용하는 데이터 필요시
  • Servers의 web.xml 631 라인 참조해서 세션 time-out 변경 가능
<session-config>
    <session-timeout>30</session-timeout>
</session-config>

 

 

 

 

✅ HttpServletRequest

  • 요청과 관련
  • 생성: 서블릿(JSP) 요청할 때
    ~
    소멸: 요청에 응답시
  • 용도: 요청 ~ 응답 범위에 사용하는 데이터 필요시
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함