J2EE 1.4に対応したweb.xmlを記述するJavaTips 〜アプリケーションサーバ/コンテナ活用編

» 2004年07月06日 10時00分 公開
[佐藤匡剛@IT]

 サーブレット 2.4/JSP 2.0を含むJ2EE 1.4から、アプリケーションサーバのデプロイメント・ディスクリプタ(web.xml)のスキーマ定義が、DTDからXML Schemaに変更になりました。

 DTDでは、定義されている順序に従って要素を記述しなくてはなりません。そのため、これまではweb.xml内の各要素(filter、servlet、servlet-mappingなど)は、DTDに定義されている順に従って記述しなくてはなりませんでした。しかし、J2EE 1.4では、web.xml直下の子要素の登場順を指定していません。

注意:ただし、Tomcat 5では、DTDとの後方互換を維持するために、DTDで定義されたものと同じ順序で要素を記述しなくてはならないことになっています。

 またDTDと異なり、XML Schemaでは複数のスキーマ定義を組み合わせて、新たなスキーマを定義することが可能です。そのため、元のスキーマ定義に矛盾しない範囲で、web.xmlを独自拡張する、といったことができるようになります。

 以前のDTDとの後方互換性が保たれているため、J2EE 1.3をベースに書かれたweb.xmlは、変更せずにそのまま利用することができます。しかし、サーブレット2.4で拡張されたリクエスト・リスナやフィルタ、JSP 2.0で追加されたタグ・ファイルや式言語(Expression Language)などの新機能を利用したいときは、web.xmlのスキーマ定義をXML Schemaに変更する必要があります。

スキーマ定義の変更方法

 J2EE 1.3のweb.xmlは以下のようになっています。赤字部分が、DTDによって宣言されているスキーマ定義になります。

J2EE 1.3のweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  ...中略...
</web-app>


 このweb.xmlを以下のように変更することで、J2EE 1.4へ対応させることができます。

J2EE 1.4のweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"
>
  ...中略...
</web-app>


web.xml内の要素

 以下に、J2EE 1.4対応のweb.xmlで使用できる主な子要素を、ツリー構造で紹介します。

<web-apps>
├─<description>*
├─<display-name>*
├─<icon>*
│  ├─<small-icon>?
│  └─<large-icon>?
├─<distributable>*
├─<context-param>*
│  ├─<description>*
│  ├─<param-name>
│  └─<param-value>
├─<filter>*
│  ├─<description>*
│  ├─<display-name>*
│  ├─<icon>*
│  │  ├─<small-icon>?
│  │  └─<large-icon>?
│  ├─<filter-name>
│  ├─<filter-class>
│  └─<init-param>*
│      ├─<description>*
│      ├─<param-name>
│      └─<param-value>
├─<filter-mapping>*
│  ├─<filter-name>
│  ├─(<url-pattern>|<servlet-name>)
│  └─<dispatcher>0..4
├─<listener>*
│  ├─<description>*
│  ├─<display-name>*
│  ├─<icon>*
│  │  ├─<small-icon>?
│  │  └─<large-icon>?
│  └─<listener-class>
├─<servlet>*
│  ├─<description>*
│  ├─<display-name>*
│  ├─<icon>*
│  │  ├─<small-icon>?
│  │  └─<large-icon>?
│  ├─<servlet-name>
│  ├─(<servlet-class>|<jsp-file>)
│  ├─<init-param>*
│  │  ├─<description>*
│  │  ├─<param-name>
│  │  └─<param-value>
│  ├─<load-on-startup>?
│  ├─<run-as>?
│  └─<security-role-ref>*
│      ├─<description>*
│      ├─<role-name>
│      └─<role-link>?
├─<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>
├─<jsp-config>*[2.4]
│  ├─<taglib>*
│  │  ├─<taglib-uri>
│  │  └─<taglib-location>
│  └─<jsp-property-group>*
├─<security-constraint>*
│  ├─<display-name>*
│  ├─<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-type>
│  └─<env-entry-value>?
├─<ejb-ref>*
│  ├─<description>*
│  ├─<ejb-ref-name>
│  ├─<ejb-ref-type>
│  ├─<home>
│  ├─<remote>
│  └─<ejb-link>?
├─<ejb-local-ref>*
│  ├─<description>*
│  ├─<ejb-ref-name>
│  ├─<ejb-ref-type>
│  ├─<local-home>
│  ├─<local>
│  └─<ejb-link>?
├─<service-ref>*[2.4]
│  ├─<description>*
│  ├─<display-name>*
│  ├─<icon>*
│  │  ├─<small-icon>?
│  │  └─<large-icon>?
│  ├─<service-ref-name>
│  ├─<service-interface>
│  ├─<wsdl-file>?
│  ├─<jaxrpc-mapping-file>?
│  ├─<service-qname>?
│  ├─<port-component-ref>*
│  └─<handler>*
├─<resource-ref>*
│  ├─<description>*
│  ├─<res-ref-name>
│  ├─<res-type>
│  ├─<res-auth>
│  └─<res-sharing-scope>?
├─<resource-env-ref>*
│  ├─<description>*
│  ├─<resource-env-ref-name>
│  └─<resource-env-ref-type>
├─<message-destination-ref>*
│  ├─<description>*
│  ├─<message-destination-ref-name>
│  ├─<message-destination-type>
│  ├─<message-destination-usage>
│  └─<message-destination-link>?
├─<message-destination>*[2.4]
│  ├─<description>*
│  ├─<display-name>*
│  ├─<icon>*
│  │  ├─<small-icon>?
│  │  └─<large-icon>?
│  └─<message-destination-name>
└─<locale-encoding-mapping-list>*[2.4]
    └─<locale-encoding-mapping>+
        ├─<locale>
        └─<encoding>


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

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

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

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

メールマガジン登録

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