Skip to content

Servlet:web.xml

Web Application의 Deployment Descriptor(환경파일 : 배포서술자, DD파일)로서 XML 형식의 파일. 모든 Web application은 반드시 하나의 web.xml 파일을 가져야 한다.

WEB-INF/web.xml에 위치한다.

web.xml 파일의 설정들은 Web Application 시작시 메모리에 로딩된다. (수정할 경우 web application을 재시작 해야 한다)

작성되는 내용

  • ServletContext의 초기 파라미터
  • Session의 유효시간 설정
  • Servlet/JSP에 대한 정의
  • Servlet/JSP 매핑
  • Mime Type 매핑
  • Welcome File list
  • Error Pages 처리
  • 리스너/필터 설정
  • 보안

서블릿 설정

<servlet>
    <servlet-name>객체의 이름</servlet-name>
    <servlet-class>객체를 생성할 클래스</servlet-class>
    <init-param>
        <param-name>파라미터 이름</param-name>
        <param-value>파라미터 변수</param-value>
    </init-param>
    <load-on-startup>시작 순서</load-on-startup>
</servlet>
  • servlet-name: servlet-mapping에 기술해 주기 위한 식별자.
  • servlet-class: 실제 서블릿 클래스, 패키지까지 정확하게 기술해야 한다.
  • init-param: 초기화 변수 목록.
  • load-on-startup: 기본적으로 서블릿은 최초 요청이 들어올때 초기화 된다. 클래스 로딩 인스턴스화 초기화설정을 해야하기 때문에, 맨처음 호출한 유져는 보통 시간보다 시간이 오래걸린다. 이 현상을 방지하기 위해 이 값을 사용한다. 0보다 크면 스타트되면서 서블릿을 초기화한다.
<servlet-mapping>
    <servlet-name>이름</servlet-name>
    <url-pattern>패턴</url-pattern>
</servlet-mapping>
  • servlet-name: </code>servlet/servlet-name</code>에 명시한 이름.
  • url-pattern: 어떠 URL경로로 접근할 수 있는지 명시한다.

ETC Setting

그 밖에 아래와 같은 설정이 존재한다.

  • session-config: 세션 기간 설정
  • mime-mapping: MIME 맵핑.
  • welcome-file-list: 시작 페이지 설정.
  • error-page: 에러 페이지 설정.
  • taglib: 태그 라이브러리 설정.
  • resource-ref: Resource 설정.
  • context-param: 루트 컨텍스트로 모든 서블릿과 필터들이 공유하는 파라미터.
  • listener: 루트 컨텍스트로 모든 서블릿과 필터들이 공유하는 리스너.

Example

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

See also

Favorite site