Skip to content
Search

Servlet:web.xml

lastmod 2018-08-17created 2015-04-177 edits

An XML file that serves as the Deployment Descriptor (configuration file: deployment descriptor, DD file) of a Web Application. Every Web application must have exactly one web.xml file.

Located at WEB-INF/web.xml.

The settings in web.xml are loaded into memory when the Web Application starts. (If you modify it, you must restart the web application.)

Contents

  • Initial parameters of the ServletContext
  • Session timeout setting
  • Servlet/JSP definitions
  • Servlet/JSP mapping
  • Mime Type mapping
  • Welcome File list
  • Error Pages handling
  • Listener/filter settings
  • Security

Servlet settings

<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: identifier used to refer to it in servlet-mapping.
  • servlet-class: the actual servlet class; must be written out exactly, including the package.
  • init-param: list of initialization variables.
  • load-on-startup: by default, a servlet is initialized when the first request comes in. Because class loading, instantiation, and initialization must be done, the first user to call it usually waits longer than normal. This value is used to prevent that. If it is greater than 0, the servlet is initialized at startup.
<servlet-mapping>
    <servlet-name>이름</servlet-name>
    <url-pattern>패턴</url-pattern>
</servlet-mapping>
  • servlet-name: the name specified in </code>servlet/servlet-name</code>.
  • url-pattern: specifies which URL path it can be accessed through.

ETC Setting

The following other settings also exist.

  • session-config: session duration setting
  • mime-mapping: MIME mapping.
  • welcome-file-list: start page setting.
  • error-page: error page setting.
  • taglib: tag library setting.
  • resource-ref: Resource setting.
  • context-param: parameters of the root context shared by all servlets and filters.
  • listener: listeners of the root context shared by all servlets and filters.

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