web.xml 문서를 조작하여 서블릿에 정보를 전달하기 위한 용도로 사용되는 인터페이스.
특징과 설정방법은 다음과 같다 :
구 분 | 적용 | 범위 설정 |
ServletConfig |
| <servlet> <servlet-name> </servlet-name> <servlet-class> </servlet-class> <init-param> <param-name> </param-name> <param-value> </param-value> </init-param> </servlet> |
ServletContext | 동일 웹 애플리케이션 내 모든 서블릿(또는 JSP)에서 사용 할 수 있다. | <context-param> <param-name> </param-name> <param-value> </param-value> </context-param> |
만약 서버의 설정을 변경해야 할 때라고 치자.
해당 값이 자바파일에 명시되어 있다면 그 클래스를 수정하여 다시 컴파일하고 배포도 다시 해야한다.
하지만 ServletConfig나 ServletContext 인터페이스를 이용해 작성하면 xml 수정 후 서버만 리셋하면 된다.
어느것이 더 편한지는 개발자 쓰기 나름
MVC2 패턴 구현에도 사용되는 듯 하다. 바로가기
● 예제
test com.test1.TestServlet name han age 20 test /test city seoul
protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletConfig config = getServletConfig(); //해당 서블릿에서만 사용가능 ServletContext context = getServletContext(); //동일한 웹어플리케이션 어디서든 접근가능 String name = config.getInitParameter("name"); String age = config.getInitParameter("age"); String city = context.getInitParameter("city"); //로그설정 context.log("로그를 출력합니드아"); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.print("name:" + name + "
"); out.print("age:" + age + "
"); out.print("city:" + city + "
"); }
'JSP > JspServlet' 카테고리의 다른 글
XML 기본구조 (0) | 2013.08.16 |
---|---|
Tomcat web.xml (0) | 2013.08.16 |
자바클래스 메서드에 접근 (0) | 2013.08.16 |
서블릿에서 out.print() (0) | 2013.08.16 |
tag : core (0) | 2013.08.16 |