서블릿 컨테이너는 서블릿 객체를 생성하고 실행하는 과정에서 다양한 객체들을 생성하여 서블릿 객체에 제공한다.
ServletConfig
웹 애플리케이션을 관리하다 보면 파일의 이름이나 경로 같은 변경될 가능성이 있는 정보를 서블릿에서 직접 설정하지 않고 외부의 설정 파일로 분리하는 경우가 있다. 그런 경우에 web.xml
파일을 통해 서블릿에서 사용하는 데이터를 분리할 수 있다. 이렇게 web.xml에 설정된 설정값을 초기화 파라미터라고 하며, ServletConfig API를 이용하여 접근할 수 있다.
서블릿당 하나의 ServletConfig가 생성되기 때문에 여러 서블릿에서 공유해서 사용하지 못하고 <init-param>
태그로 등록된 서블릿에서만 사용이 가능하다.
<!-- web.xml 설정 -->
<servlet>
<servlet-name>서블릿명</servlet-name>
<servlet-class>패키지를 포함한 서블릿명</servlet-class>
<init-param>
<param-name>초기화 파라미터 이름</param-name>
<param-value>초기화 파라미터 값</param-value>
</init-param>
<init-param>
<param-name>초기화 파라미터 이름</param-name>
<param-value>초기화 파라미터 값</param-value>
</init-param>
...
</servlet>
메서드명 | 리턴타입 | 내용 |
---|---|---|
getInitParameter(name) | String | name에 해당되는 초기화 파라미터 값 리턴 |
getServletName() | String | 요청한 서블릿의 이름 리턴 |
getInitParameterNames() | Enumeration | 모든 초기화 파라미터 name 값을 리턴 |
⌛️ 실습
특정 경로와 email 정보를 외부의 설정 파일로 분리하여 저장
<!--web.xml-->
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.servlet.TestServlet</servlet-class>
<init-param>
<param-name>dir_path</param-name>
<param-value>c://temp</param-value>
</init-param>
<init-param>
<param-name>email</param-name>
<param-value>aaa@naver.com</param-value>
</init-param>
</servlet>
// TestServlet.java
package com.servlet;
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("TestServlet"+ getServletName());
String dir_path = getInitParameter("dir_path");
String email = getInitParameter("email");
System.out.println(dir_path + "\t" + email);
}
}
ServletContext
ServletConfig
가 하나의 서블릿과 1대1 대응하는 객체라면 ServletContext
는 여러 서블릿이 공유해서 사용하는 객체이다. 여러 서블릿에서 접근 가능한 컨텍스트 파라미터 (context parameter)를 사용한다.
ServletContext객체를 얻기 위해서는 서블릿의 doGet
메서드에서 getServletContext()
메서드를 사용한다.
ServletContext ctx = getServletContext();
메서드명 | 리턴 타입 | 내용 |
---|---|---|
getInitParameter(name) | String | name에 해당하는 컨텍스트 파라미터 값 리턴 |
getAttribute(name) | Object | name에 해당되는 속성 값 리턴 |
setAttribute(name, value) | void | application scope 해당되는 속성 값을 저장할 때 사용 |
getResourceAsStream(path) | InputStream | 웹 어플리케이션의 path 경로에 해당되는 파일을 읽기모드로 접근 |
[Servlet] 서블릿 스코프 (Scope)
Servlet에서 변수를 지정하고 객체에 담아 포워드하려면 객체가 어디까지 유지되는지 반드시 알아야한다. Servlet 스코프(scope)는 서블릿 속성에 접근할 수 있는 범위를 의미하며 웹 어플리케이션에
gangintheremark.tistory.com
⌛️ 실습
특정 id와 passwd를 정보를 외부의 설정 파일로 분리하여 저장
<!-- web.xml -->
<context-param>
<param-name>userid</param-name>
<param-value>abcd</param-value>
</context-param>
<context-param>
<param-name>passwd</param-name>
<param-value>1234</param-value>
</context-param>
// Servlet.java
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// ServletContext의 getInitParameter(name)
ServletContext ctx = getServletContext();
String userid = ctx.getInitParameter("userid");
String passwd = ctx.getInitParameter("passwd");
System.out.println(userid + "\t" + passwd);
}
정리
ServletConfig 객체는 서블릿 당 하나씩 생성하며 다른 서블릿과 공유할 수 없다. ServletContext 객체는 웹 어플리케이션 당 한 개만 생성하여 서블릿 컨테이너가 생성한 모든 객체에서 접근할 수 있다. 두 객체 모두 web.xml에 <key, value> 쌍으로 저장한다.
'WebServer > JSP&Servlet' 카테고리의 다른 글
[Servlet] Filter API (0) | 2023.08.10 |
---|---|
[Servlet] 서블릿 스코프 (Scope) (0) | 2023.08.10 |
[Servlet] Servlet 요청 및 응답 처리 (0) | 2023.08.09 |
[Servlet] Servlet 컨테이너와 계층구조, 라이프 사이클(Lifecycle) (0) | 2023.08.09 |
[Servlet] Servlet 작성 규칙과 서블릿 매핑 (0) | 2023.08.09 |