Tomcat 部署 Web 應用時遇到 HTTP 404 - The requested resource is not available 是最常見問題。本文系統梳理 5 大核心原因與對應解決方案,助你快速定位修復。
https://localhost:8080/projectName/resource 404projectName (context-path) 是否正確,默認等於 WAR 包名或部署目錄名web.xml 中 <url-pattern> 與 Controller 映射是否匹配DispatcherServlet 映射路徑(如 *.do//)// 錯誤示範
response.sendRedirect("loginSuccess.jsp"); // 相對路徑,易出錯
request.getRequestDispatcher("/WEB-INF/jsp/x.jsp").forward(...); // 轉發路徑錯
// 正確寫法
// sendRedirect:需完整路徑(含 context-path)
response.sendRedirect(request.getContextPath() + "/Manage/loginSuccess.jsp");
// 或
response.sendRedirect("/ProjectName/Manage/loginSuccess.jsp");
// forward:相對當前 Web 應用根目錄
request.getRequestDispatcher("/WEB-INF/jsp/loginSuccess.jsp").forward(request, response);
口訣:sendRedirect 加項目名,forward 不加項目名,均以 / 開頭表示 Web 應用根。
webapps/項目名/ 下是否存在 JSP、HTML、CSS、JS 等靜態資源pom.xml <packaging>war</packaging> 且資源在 src/main/webappweb.xml 中 <servlet-mapping> URL 模式與請求不匹配@RequestMapping、@GetMapping 路徑/)jakarta.servlet.*),舊代碼 javax.servlet.* 需遷移或降級 Tomcat 9logs/localhost.yyyy-mm-dd.log 搜 ERROR/manager/html 確認應用狀態為 Runninghttps://host:port/project/test.html 驗證部署掌握路徑規則、區分重定向與轉發、驗證部署產物,是解決 404 的三板斧。