web.xml内の記述順を知る(サーブレット2.3)JavaTips 〜アプリケーションサーバ/コンテナ活用編

» 2004年06月22日 10時00分 公開
[山田祥寛@IT]

 デプロイメント・ディスクリプタ(web.xml)は、Webアプリケーション単位のコンテナへの配置設定を記述するための設定ファイルです。

 例えば、Tomcatの場合、コンテナ共通の設定を行う場合には「/conf/web.xml」を、アプリケーション個々の設定を行う場合には「/webapps/アプリケーション名/WEB-INF/web.xml」を編集します。

注意:Tomcat全体の挙動を左右する「/conf/web.xml」の設定は、原則として個々のアプリケーション開発者が変更すべきではありません。

 ファイル名からも分かるとおり、web.xmlの中身はXML形式のテキストファイルです。可視性にも富んでおり、設定内容も決して難しいことはありません。しかし、設定を誤ってしまうと、アプリケーション全体が動作しなくなる場合があるため、編集には十分な注意が必要です。

 特に、サーブレット2.3のweb.xmlでは、スキーマ定義としてDTDが用いられており、ルート(最上位)要素であるweb-apps要素配下に配置する子要素の順番が厳密に決められています。そのため、この順番を間違えただけでもエラーとなってしまうので、編集の際は十分に注意してください。

 サーブレット2.3のweb.xmlで使用できる主な子要素と、その登場順について、以下にまとめておきます。

<web-apps>
├─<icon>?
│      ├─<small-icon>?
│      └─<large-icon>?
├─<display-name>?
├─<description>?
├─<distributable>?
├─<context-param>*
│      ├─<param-name>
│      ├─<param-value>
│      └─<description>?
├─<filter>*[2.3]
│      ├─<icon>?
│      │      ├─<small-icon>?
│      │      └─<large-icon>?
│      ├─<filter-name>
│      ├─<display-name>?
│      ├─<description>?
│      ├─<filter-class>
│      └─<init-param>*
│              ├─<param-name>
│              ├─<param-value>
│              └─<description>?
├─<filter-mapping>*[2.3]
│      ├─<filter-name>
│      └─(<url-pattern>|<servlet-name>)
├─<listener>*[2.3]
│      └─<listener-class>
├─<servlet>*
│      ├─<icon>?
│      │      ├─<small-icon>?
│      │      └─<large-icon>?
│      ├─<servlet-name>
│      ├─<display-name>?
│      ├─<description>?
│      ├─(<servlet-class>|<jsp-file>)
│      ├─<init-param>*
│      │      ├─<param-name>
│      │      ├─<param-value>
│      │      └─<description>?
│      ├─<load-on-startup>?
│      ├─<run-as>?[2.3]
│      └─<security-role-ref>*
├─<servlet-mapping>*
│      ├─<servlet-name>
│      └─<url-pattern>
├─<session-config>?
│      └─<session-timeout>?
├─<mime-mapping>*
│      ├─<extension>
│      └─<mime-type>
├─<welcome-file-list>?
│      └─<welcome-file>+
├─<error-page>*
│      ├─(<error-code>|<exception-type>)
│      └─<location>
├─<taglib>*
│      ├─<taglib-uri>
│      └─<taglib-location>
├─<resource-env-ref>*[2.3]
│      ├─<description>?
│      ├─<resource-env-ref-name>
│      └─<resource-env-ref-type>
├─<resource-ref>*
│      ├─<description>?
│      ├─<res-ref-name>
│      ├─<res-type>
│      ├─<res-auth>
│      └─<res-sharing-scope>?[2.3]
├─<security-constraint>*
│      ├─<display-name>?[2.3]
│      ├─<web-resource-collection>+
│      │      ├─<web-resource-name>
│      │      ├─<description>?
│      │      ├─<url-pattern>*
│      │      └─<http-method>*
│      ├─<auth-constraint>?
│      │      ├─<description>?
│      │      └─<role-name>*
│      └─<user-data-constraint>?
│              ├─<description>?
│              └─<transport-guarantee>
├─<login-config>?
│      ├─<auth-method>?
│      ├─<realm-name>?
│      └─<form-login-config>?
│              ├─<form-login-page>
│              └─<form-error-page>
├─<security-role>*
│      ├─<description>?
│      └─<role-name>
├─<env-entry>*
│      ├─<description>?
│      ├─<env-entry-name>
│      ├─<env-entry-value>?
│      └─<env-entry-type>
├─<ejb-ref>*
│      ├─<description>?
│      ├─<ejb-ref-name>
│      ├─<ejb-ref-type>
│      ├─<home>
│      ├─<remote>
│      └─<ejb-link>?
└─<ejb-local-ref>*[2.3]
        ├─<description>?
        ├─<ejb-ref-name>
        ├─<ejb-ref-type>
        ├─<local-home>
        ├─<local>
        └─<ejb-link>?

 各要素の右側の記号は、各要素の登場回数を示すもので、それぞれの意味は以下の表のとおりです。また、[2.3]は当該要素がサーブレット2.3以降でのみ利用可能であることを示します。

要素右側の記号の意味
記号 意味
なし 1回のみ登場
* 0回以上登場
+ 1回以上登場
? 0、または1回登場

 各子要素の使い方については、別稿にて紹介します。

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。