專案名/WebContent/WEB-INF/classes專案根目錄/ ├── src/ # Java 原始碼 ├── WebContent/ # Web 根目錄 (部署根) │ ├── index.jsp # 歡迎頁面 │ ├── css/ # 樣式檔案 │ ├── js/ # JavaScript │ ├── images/ # 圖片資源 │ └── WEB-INF/ # 客戶端不可直接存取 │ ├── web.xml # 部署描述符 (核心配置) │ ├── classes/ # 編譯後類別檔案 │ └── lib/ # 依賴 JAR 包
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee
https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>MyWebApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Servlet 定義範例 -->
<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>
</web-app>
WebContent/WEB-INF/lib/ 建立 lib 目錄servlet-api.jar、jdbc-driver.jar、框架 JAR)Web App Libraries 庫servlet-api.jar 編譯 Servlet 程式碼provided 作用域,Eclipse 手動管理時勿放入 lib$CATALINA_HOMEwebapps/ 目錄mkdir MyWebAppWebContent/ **所有內容** 複製到此目錄(不是複製 WebContent 目錄本身)webapps/MyWebApp/index.jsp、webapps/MyWebApp/WEB-INF/web.xmlbin/startup.sh (Linux/Mac) 或 bin/startup.bat (Windows)https://localhost:8080/MyWebApp/<project xmlns="https://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-webapp</artifactId>
<version>1.0.0</version>
<packaging>war</packaging> <!-- 關鍵:war 打包 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- Servlet 3.0+ 可選 -->
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope> <!-- 關鍵:provided 作用域 -->
</dependency>
<!-- 其他依賴 -->
</dependencies>
<build>
<finalName>my-webapp</finalName> <!-- WAR 檔名 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory> <!-- 標準目錄 -->
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/
├── main/
│ ├── java/ # Java 原始碼
│ ├── resources/ # 資源檔案 (複製到 classes)
│ └── webapp/ # Web 根目錄 (對應 WebContent)
│ ├── WEB-INF/
│ │ └── web.xml
│ ├── index.jsp
│ └── resources/ # 靜態資源
└── test/
├── java/
└── resources/
| 問題 | 原因 | 解決 |
|---|---|---|
| 404 Not Found | 路徑錯誤、web.xml 缺失、歡迎檔案未配置 | 檢查 URL、web.xml、welcome-file-list |
| ClassNotFoundException | JAR 未放入 WEB-INF/lib、Build Path 未加入 | 確認 lib 目錄、Build Path、Maven 依賴 |
| Servlet 初始化失敗 | servlet-api.jar 衝突、版本不匹配 | 設定 provided 作用域、檢查容器版本 |
| 中文亂碼 | 編碼不一致 | web.xml 配置 filter、Tomcat server.xml URIEncoding=UTF-8、JSP pageEncoding |
| 靜態資源 404 | DefaultServlet 映射衝突 | web.xml 配置 <url-pattern>/</url-pattern> 而非 /* |
FROM tomcat:9-jre8 + COPY target/*.war /usr/local/tomcat/webapps/Java Project 轉 Web Project 核心在於目錄結構標準化(WebContent/WEB-INF)、輸出路徑配置、web.xml 描述符、依賴管理。現代開發強烈建議直接使用 Maven/Gradle + war 打包,配合 IDE 整合部署,建立標準化、可重現、易維護的專案骨架。