1. ServletConfig의 역할
- 서블릿 초기화 파라미터(Initialization Parameter) 접근:
- web.xml (배포 설명자) 또는 어노테이션(@WebInitParam)을 통해 설정된 서블릿별 초기화 파라미터 값을 읽어올 수 있다.
- 초기화 파라미터는 해당 서블릿 내에서만 사용되는 설정 값. (웹 애플리케이션 전체에서 공유되는 ServletContext의 context parameter와는 다름)
- 서블릿 이름 획득:
- 서블릿에 할당된 이름(논리적인 이름)을 얻을 수 있다. (<servlet-name> in web.xml 또는 @WebServlet의 name 속성)
- ServletContext 객체 접근:
- ServletConfig 객체를 통해 ServletContext 객체에 접근할 수 있다.
2. ServletConfig 객체 얻는 방법
ServletConfig 객체는 서블릿 컨테이너가 서블릿의 init() 메서드를 호출할 때 매개변수로 전달한다.
Java
public class MyServlet extends HttpServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config); // 필수! (부모 클래스의 init() 호출)
this.config = config; // ServletConfig 객체 저장 (필요한 경우)
String myParam = config.getInitParameter("myParam");
}
// ... (doGet, doPost 등)
}
//혹은 GenericServlet을 상속받는다면, getServletConfig()로 바로 접근 가능.
- GenericServlet을 상속받는 경우 getServletConfig() 메서드를 사용하여 언제든지 ServletConfig 객체에 접근할 수 있다.
- HttpServlet을 상속받는 경우에는 일반적으로 init(ServletConfig config) 메서드에서 받은 config를 필드에 저장해두고 사용하거나, super.init(config)를 호출한 후 getServletConfig()를 사용.
3. ServletConfig의 주요 메서드
메서드반환 타입설명
getInitParameter(String name) | String | web.xml 또는 어노테이션에 설정된 서블릿 초기화 파라미터 중 지정된 이름(name)의 값을 반환. 없으면 null을 반환. |
getInitParameterNames() | Enumeration<String> | web.xml 또는 어노테이션에 설정된 모든 서블릿 초기화 파라미터의 이름들을 Enumeration 형태로 반환. |
getServletName() | String | 서블릿의 이름(논리적인 이름)을 반환. (<servlet-name> in web.xml 또는 @WebServlet의 name 속성) |
getServletContext() | ServletContext | 웹 애플리케이션의 ServletContext 객체를 반환. |
4. 사용 예시
- web.xml을 사용한 초기화 파라미터 설정:
-
XML
<web-app> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.example.MyServlet</servlet-class> <init-param> <param-name>databaseDriver</param-name> <param-value>com.mysql.cj.jdbc.Driver</param-value> </init-param> <init-param> <param-name>maxConnections</param-name> <param-value>100</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myservlet</url-pattern> </servlet-mapping> </web-app>
- 어노테이션을 사용한 초기화 파라미터 설정:
-
Java
@WebServlet( name = "MyServlet", urlPatterns = {"/myservlet"}, initParams = { @WebInitParam(name = "databaseDriver", value = "com.mysql.cj.jdbc.Driver"), @WebInitParam(name = "maxConnections", value = "100") } ) public class MyServlet extends HttpServlet { // ... }
- 서블릿에서 초기화 파라미터 읽기:
-
Java
public class MyServlet extends HttpServlet { private String dbDriver; private int maxConnections; @Override public void init(ServletConfig config) throws ServletException { super.init(config); dbDriver = config.getInitParameter("databaseDriver"); maxConnections = Integer.parseInt(config.getInitParameter("maxConnections")); // ... dbDriver와 maxConnections를 사용하여 데이터베이스 연결 설정 ... } //혹은 GenericServlet 상속 시 //String dbDriver = getServletConfig().getInitParameter("databaseDriver"); // ... (doGet, doPost 등) }
'개발 > servlet' 카테고리의 다른 글
MVC 패턴 (0) | 2025.03.05 |
---|---|
HttpSession 과 Cookie 인터페이스 정리 (0) | 2025.03.05 |
ServletContext 인터페이스 정리 (0) | 2025.03.05 |
Servlet RequestDispatcher 정리 (0) | 2025.03.05 |
Servlet Request 객체와 Response의 주요 메서드 (0) | 2025.03.05 |