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) 요청할 때
~
소멸: 요청에 응답시 - 용도: 요청 ~ 응답 범위에 사용하는 데이터 필요시
'Programming Language > SERVLET' 카테고리의 다른 글
서블릿 (servlet) - DB연동 (0) | 2023.08.11 |
---|---|
서블릿 (servlet) - Filter API 활용 (0) | 2023.08.10 |
서블릿 (servlet) - ServletConfig vs ServletContext (0) | 2023.08.10 |
서블릿 (servlet) - 서블릿의 핵심 기능 (1) | 2023.08.09 |
서블릿 (servlet) - 서블릿 lifecycle (0) | 2023.08.09 |