web.xmlって何?Webアプリの設定書

JavaでWebアプリケーションを作っていると、「web.xml」というファイルに出会います。
web.xmlは、JavaのWebアプリケーションの動作を設定するファイルです。
正式にはデプロイメント記述子(Deployment Descriptor)と呼ばれます。
身近な例で理解しよう
web.xmlを理解するために、レストランに例えてみましょう。
レストラン(Webアプリケーション):
web.xml(営業マニュアル)
├─ 営業時間の設定
├─ メニュー(URL)と厨房(サーブレット)の対応表
├─ お客様の案内係(フィルター)の配置
├─ エラー時の対応手順
└─ セキュリティルール
web.xmlは、Webアプリケーションがどう動くべきか記載された「マニュアル」なんです。
web.xmlの役割
主な役割:
1. サーブレットの設定
- どのURLでどのサーブレットを実行するか
- サーブレットの初期化パラメータ
2. フィルターの設定
- リクエストの前処理・後処理
- 文字コード変換、ログ記録、認証チェック
3. セッション管理
- セッションタイムアウト時間
- Cookie設定
4. エラーページの設定
- 404エラー時のページ
- 500エラー時のページ
5. セキュリティ設定
- アクセス制限
- 認証方法
6. ウェルカムファイル
- デフォルトで表示するページ
- index.htmlやindex.jspなど
web.xmlの場所
標準的な配置:
Webアプリケーション/
├─ WEB-INF/
│ ├─ web.xml ← ここ!
│ ├─ classes/ (コンパイル済みクラス)
│ └─ lib/ (JARライブラリ)
├─ index.jsp
└─ css/
重要なポイント:
- WEB-INFディレクトリ内に配置
- ファイル名は必ず「web.xml」
- 外部から直接アクセスできない(セキュリティ)
web.xmlの基本構造
実際のweb.xmlファイルの構造を見ていきましょう。
最小限のweb.xml
最もシンプルな例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- ここに設定を書く -->
</web-app>
各部分の説明:
- :XML宣言
- :ルート要素
- xmlns:XML名前空間の定義
- version=”4.0″:Servlet APIのバージョン
バージョンの違い
主なバージョン:
| バージョン | Java EE | 主な特徴 |
|---|---|---|
| 2.3 | J2EE 1.3 | 古い標準 |
| 2.4 | J2EE 1.4 | web.xmlの簡素化 |
| 2.5 | Java EE 5 | アノテーションサポート |
| 3.0 | Java EE 6 | web.xml不要に(オプション化) |
| 3.1 | Java EE 7 | 非同期処理の強化 |
| 4.0 | Java EE 8 | HTTP/2対応 |
重要な変化:
Servlet 3.0以降では、アノテーションだけで設定できるようになりました。
web.xmlはオプションになっています。
サーブレットの設定
web.xmlの最も基本的な使い方は、サーブレットの設定です。
基本的なサーブレット設定
<web-app>
<!-- サーブレットの定義 -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<!-- URLマッピング -->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
動作の流れ:
ユーザーが/helloにアクセス
↓
HelloServletが呼ばれる
↓
com.example.HelloServletクラスが実行される
初期化パラメータの設定
サーブレットに設定値を渡すことができます。
<servlet>
<servlet-name>ConfigServlet</servlet-name>
<servlet-class>com.example.ConfigServlet</servlet-class>
<!-- 初期化パラメータ -->
<init-param>
<param-name>adminEmail</param-name>
<param-value>admin@example.com</param-value>
</init-param>
<init-param>
<param-name>maxUsers</param-name>
<param-value>100</param-value>
</init-param>
<!-- 起動時にロード -->
<load-on-startup>1</load-on-startup>
</servlet>
サーブレット側での取得:
public class ConfigServlet extends HttpServlet {
private String adminEmail;
private int maxUsers;
@Override
public void init() throws ServletException {
adminEmail = getServletConfig().getInitParameter("adminEmail");
maxUsers = Integer.parseInt(
getServletConfig().getInitParameter("maxUsers")
);
}
}
の意味:
- 数字が小さいほど先に起動
- 省略すると、最初のアクセス時に起動
- 初期化に時間がかかるサーブレットに使用
複数のURLパターン
1つのサーブレットに複数のURLを割り当てられます。
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/profile/*</url-pattern>
</servlet-mapping>
ワイルドカードのパターン:
/user/*:/user/で始まるすべてのURL*.do:.doで終わるすべてのURL/:デフォルトサーブレット(他にマッチしない場合)
フィルターの設定
フィルターは、リクエストの前処理や後処理を行います。
基本的なフィルター設定
<web-app>
<!-- フィルターの定義 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.example.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- フィルターのマッピング -->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
動作の流れ:
リクエスト
↓
フィルター(前処理)
↓
サーブレット
↓
フィルター(後処理)
↓
レスポンス
実用的なフィルター例
文字エンコーディングフィルター:
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.example.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ログ記録フィルター:
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>com.example.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
認証チェックフィルター:
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.example.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
フィルターの実行順序
複数のフィルターがある場合、web.xmlに記述された順番で実行されます。
<!-- この順番で実行される -->
<filter-mapping>
<filter-name>EncodingFilter</filter-name> <!-- 1番目 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>LoggingFilter</filter-name> <!-- 2番目 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name> <!-- 3番目 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
リスナーの設定

リスナーは、特定のイベントが発生した時に動作します。
アプリケーション起動時の処理
<listener>
<listener-class>com.example.AppInitListener</listener-class>
</listener>
リスナークラスの例:
public class AppInitListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// アプリケーション起動時の処理
System.out.println("アプリケーションが起動しました");
// データベース接続プールの初期化など
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// アプリケーション終了時の処理
System.out.println("アプリケーションが終了しました");
// リソースのクリーンアップなど
}
}
セッションリスナー
<listener>
<listener-class>com.example.SessionListener</listener-class>
</listener>
セッションの作成・破棄を監視:
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("セッションが作成されました: " + se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("セッションが破棄されました: " + se.getSession().getId());
}
}
エラーページの設定
エラーが発生した時に表示するページを指定できます。
HTTPステータスコード別
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error/403.jsp</location>
</error-page>
よく使うエラーコード:
- 404:ページが見つからない
- 500:サーバー内部エラー
- 403:アクセス禁止
- 401:認証が必要
例外タイプ別
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error/null-pointer.jsp</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/error/database-error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error/general-error.jsp</location>
</error-page>
処理の優先順位:
- 具体的な例外タイプ
- より一般的な例外タイプ
- エラーコード
- デフォルトエラーページ
セッション設定
セッション管理に関する設定です。
セッションタイムアウト
<session-config>
<!-- タイムアウト時間(分単位) -->
<session-timeout>30</session-timeout>
</session-config>
デフォルト値:
- 多くのサーバーでは30分
- 0または負の値:タイムアウトなし(非推奨)
Cookie設定
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>MYSESSIONID</name>
<http-only>true</http-only>
<secure>true</secure>
<max-age>3600</max-age>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
各設定の意味:
- name:セッションCookieの名前
- http-only:JavaScriptからのアクセスを防ぐ
- secure:HTTPS通信のみで送信
- max-age:Cookieの有効期間(秒)
- tracking-mode:セッション追跡方法(COOKIE、URL、SSL)
ウェルカムファイルの設定
トップページのファイルを指定します。
基本設定
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
動作:
ユーザーが http://example.com/ にアクセス
↓
1. index.htmlがあれば表示
2. なければindex.jspを探す
3. なければdefault.htmlを探す
4. すべてなければ404エラー
コンテキストパラメータ
アプリケーション全体で使えるパラメータです。
設定方法
<context-param>
<param-name>databaseURL</param-name>
<param-value>jdbc:mysql://localhost:3306/mydb</param-value>
</context-param>
<context-param>
<param-name>maxConnections</param-name>
<param-value>50</param-value>
</context-param>
<context-param>
<param-name>applicationMode</param-name>
<param-value>production</param-value>
</context-param>
Javaでの取得
サーブレットから:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = getServletContext();
String dbURL = context.getInitParameter("databaseURL");
String mode = context.getInitParameter("applicationMode");
// 使用する
System.out.println("Database URL: " + dbURL);
System.out.println("Mode: " + mode);
}
}
リスナーから:
public class AppInitListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String dbURL = context.getInitParameter("databaseURL");
// 初期化処理で使用
}
}
セキュリティ設定
アクセス制限や認証の設定ができます。
ベーシック認証
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin Area</web-resource-name>
<url-pattern>/admin/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Administration</realm-name>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
動作:
/admin/にアクセス
↓
ユーザー名とパスワードを要求
↓
adminロールを持つユーザーのみアクセス可能
HTTPS強制
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
transport-guaranteeの値:
- NONE:制限なし
- INTEGRAL:データ改ざん防止
- CONFIDENTIAL:暗号化必須(HTTPS)
MIMEタイプの設定
ファイル拡張子とMIMEタイプの対応を定義します。
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xml</extension>
<mime-type>application/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
使用例:
- カスタムファイル形式の定義
- デフォルトMIMEタイプの上書き
web.xmlとアノテーションの比較
Servlet 3.0以降では、アノテーションでも設定できます。
サーブレットの設定
web.xml:
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
アノテーション:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 処理
}
}
フィルターの設定
web.xml:
<filter>
<filter-name>LoggingFilter</filter-name>
<filter-class>com.example.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
アノテーション:
@WebFilter("/*")
public class LoggingFilter implements Filter {
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// 処理
chain.doFilter(request, response);
}
}
どちらを使うべき?
web.xmlのメリット:
- 設定が一箇所に集約
- コードを変更せずに設定変更可能
- 複雑な設定がしやすい
- 環境別設定の切り替えが容易
アノテーションのメリット:
- コードと設定が近くにある
- 記述量が少ない
- タイプセーフ
推奨:
- 基本的な設定:アノテーション
- 環境依存の設定:web.xml
- セキュリティ設定:web.xml
- 複雑な設定:web.xml
実践的なweb.xml例

実際のプロジェクトで使える完全な例です。
小規模Webアプリケーション
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>My Web Application</display-name>
<description>Sample Web Application</description>
<!-- コンテキストパラメータ -->
<context-param>
<param-name>databaseURL</param-name>
<param-value>jdbc:mysql://localhost:3306/mydb</param-value>
</context-param>
<!-- 文字エンコーディングフィルター -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.example.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- リスナー -->
<listener>
<listener-class>com.example.listener.AppInitListener</listener-class>
</listener>
<!-- セッション設定 -->
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
<!-- ウェルカムファイル -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- エラーページ -->
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
</web-app>
大規模エンタープライズアプリケーション
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Enterprise Application</display-name>
<!-- Spring Framework設定 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 文字エンコーディング -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- セキュリティ設定 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin Area</web-resource-name>
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login-error.jsp</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
トラブルシューティング
web.xmlでよくある問題と解決方法です。
XMLの構文エラー
問題:
アプリケーションが起動しない
原因:
- タグの閉じ忘れ
- 名前空間の誤り
- 要素の順序違反
解決方法:
- XMLバリデーションツールを使う
IDEの検証機能を使用するか、オンラインツールでチェック。
- ログを確認
Tomcatのログ: logs/catalina.out
エラーメッセージから原因を特定。
- 要素の順序を確認
web.xmlの要素には記述順序のルールがあります:
<web-app>
<!-- 1. display-name, description -->
<!-- 2. context-param -->
<!-- 3. filter -->
<!-- 4. filter-mapping -->
<!-- 5. listener -->
<!-- 6. servlet -->
<!-- 7. servlet-mapping -->
<!-- 8. session-config -->
<!-- 9. welcome-file-list -->
<!-- 10. error-page -->
<!-- 11. security-constraint -->
<!-- 12. login-config -->
<!-- 13. security-role -->
</web-app>
サーブレットが呼ばれない
問題:
URLにアクセスしても404エラー
確認ポイント:
- URLパターンの確認
<!-- 正しい -->
<url-pattern>/hello</url-pattern>
<!-- 間違い(スラッシュなし) -->
<url-pattern>hello</url-pattern>
- servlet-nameの一致
<servlet>
<servlet-name>MyServlet</servlet-name> <!-- 名前を一致させる -->
...
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name> <!-- 同じ名前 -->
...
</servlet-mapping>
- クラスパスの確認
<servlet-class>com.example.MyServlet</servlet-class>
パッケージ名を含む完全修飾名が正しいか確認。
フィルターが動作しない
原因:
- filter-mappingの記述順序
- URLパターンのミスマッチ
解決方法:
<!-- filterとfilter-mappingの順序を守る -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.example.MyFilter</filter-class>
</filter>
<!-- filterの後にfilter-mapping -->
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
セッションタイムアウトが効かない
確認:
<!-- 分単位で指定 -->
<session-config>
<session-timeout>30</session-timeout> <!-- 30分 -->
</session-config>
注意点:
- 単位は「分」
- 0または負の値はタイムアウトなし
- サーバー全体の設定が優先される場合がある
ベストプラクティス
効果的なweb.xmlの書き方です。
コメントを活用
<!-- ========================================
文字エンコーディング設定
すべてのリクエスト/レスポンスをUTF-8に統一
======================================== -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.example.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
設定の整理
グループ化:
<web-app>
<!-- ========== パラメータ ========== -->
<context-param>...</context-param>
<!-- ========== フィルター ========== -->
<filter>...</filter>
<filter-mapping>...</filter-mapping>
<!-- ========== リスナー ========== -->
<listener>...</listener>
<!-- ========== サーブレット ========== -->
<servlet>...</servlet>
<servlet-mapping>...</servlet-mapping>
<!-- ========== セキュリティ ========== -->
<security-constraint>...</security-constraint>
</web-app>
環境別設定の分離
開発環境と本番環境で設定を切り替える:
方法1:Maven/Gradleのプロファイル
<!-- 開発環境用 web.xml -->
<context-param>
<param-name>databaseURL</param-name>
<param-value>jdbc:mysql://localhost:3306/mydb_dev</param-value>
</context-param>
<!-- 本番環境用 web.xml -->
<context-param>
<param-name>databaseURL</param-name>
<param-value>jdbc:mysql://production-server:3306/mydb</param-value>
</context-param>
ビルド時に適切なweb.xmlを選択。
方法2:外部設定ファイル
<context-param>
<param-name>configLocation</param-name>
<param-value>/etc/myapp/config.properties</param-value>
</context-param>
環境固有の値は外部ファイルに記述。
セキュリティの考慮
<!-- セッションCookieの保護 -->
<session-config>
<cookie-config>
<http-only>true</http-only> <!-- XSS対策 -->
<secure>true</secure> <!-- HTTPS限定 -->
</cookie-config>
</session-config>
<!-- HTTPS強制 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
まとめ:web.xmlを使いこなそう
この記事では、web.xmlについて基礎から実践まで解説してきました。
重要なポイントのおさらい:
- web.xmlはJavaウェブアプリケーションの設定ファイル
- WEB-INFディレクトリに配置する
- サーブレット、フィルター、リスナーの設定が可能
- エラーページやセッション管理も設定できる
- Servlet 3.0以降はアノテーションでも設定可能
- 環境依存の設定や複雑な設定はweb.xmlが適している
- XMLの記述順序には決まりがある
- コメントや整理で読みやすくする
web.xmlが必要な場面:
- レガシーアプリケーション(Servlet 2.x)
- 環境別設定の切り替え
- 複雑なセキュリティ設定
- フレームワーク(Spring、Struts等)の設定
- 詳細なフィルター順序制御
アノテーションが適している場面:
- 新規プロジェクト(Servlet 3.0以降)
- シンプルなサーブレット設定
- 開発の高速化
- コードと設定の一体化
今後の学習:
- 実際にweb.xmlを書いてみる
- サーブレットやフィルターと組み合わせる
- Spring BootなどのフレームワークでJava EEの理解を深める
- アノテーションベースの設定も学ぶ
web.xmlは古いと思われがちですが、エンタープライズアプリケーションでは今でも重要な役割を果たしています。
基本をしっかり理解して、状況に応じて適切に使い分けられるようになりましょう!

コメント