You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌入式Tomcat部署Java智能家居Web应用遇HTTP 404错误求助

Hey there, let’s work through this 404 error together—those can be frustrating, but they almost always trace back to a handful of common misconfigurations. Here’s a step-by-step troubleshooting plan tailored to your smart home app setup:

1. Confirm Tomcat Deployment Status

First, rule out whether your app is even deployed correctly:

  • Fire up the Tomcat Manager (usually at localhost:8080/manager/html), log in, and check if your smartHouse app appears under "Deployed Applications". If it’s missing entirely, your deployment failed—skip ahead to the POM and build checks.
  • If it is listed, verify the Context Path matches what you’re trying to access (/smartHouse). Tomcat sometimes auto-assigns a different path if there’s a naming conflict or build artifact mismatch (like a version number appended to the WAR name).
2. Validate Servlet Mapping Configuration

Since your Main servlet is the entry point, double-check its mapping:

  • If you’re using web.xml:
    Ensure your <servlet> and <servlet-mapping> blocks are correctly defined, with no typos in package or class names. Example:
    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.yourpackage.Main</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MainServlet</servlet-name>
        <url-pattern>/</url-pattern> <!-- Or /smartHouse/*, depending on your intended path -->
    </servlet-mapping>
    
  • If you’re using Servlet 3.0+ annotations:
    Confirm your Main class has @WebServlet("/") or @WebServlet("/smartHouse/*")—the URL pattern needs to align exactly with what you’re trying to access in the browser.
  • Critical check: Make sure the fully qualified class name in the mapping matches where your compiled Main.class file lives in the project (no mismatched package names!).
3. Audit Maven POM Settings

Your pom.xml controls how your WAR is built—small mistakes here can break deployment:

  • Ensure the <packaging> tag is set to war (not jar). Tomcat can’t deploy JAR files as web apps.
  • Check the <finalName> in the <build> section:
    <build>
        <finalName>smartHouse</finalName>
    </build>
    
    This name sets Tomcat’s default context path. If it’s set to something like smartHouse-v1, your URL would need to be localhost:8080/smartHouse-v1 instead.
  • Mark the Servlet API dependency as provided:
    Tomcat already includes this library, so bundling it with your WAR causes conflicts. Example:
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version> <!-- Match your Tomcat version -->
        <scope>provided</scope>
    </dependency>
    
4. Check Project Structure & Build Output

Maven expects a standard web app structure—deviations can cause missing resources:

  • Confirm your project follows this layout:
    src/
      main/
        java/
          com/
            yourpackage/
              Main.java
        webapp/
          WEB-INF/
            web.xml
          (static files like index.html go here)
    
  • Run mvn clean package to rebuild your WAR, then unzip the file in target/ to verify:
    • The WEB-INF/classes/ folder contains your compiled Main.class (in the correct package hierarchy).
    • The WEB-INF/web.xml (if used) has the exact servlet mappings you defined.
  • If deploying from an IDE (IntelliJ/Eclipse), make sure it’s configured to deploy the full WAR artifact, not a partial build.
5. Dig Into Logs & Test Basic Endpoints

Logs are your best friend for debugging 404s:

  • Check Tomcat’s logs/ directory (usually in your Tomcat installation folder). Look at localhost.log and localhost_access_log.*—these will show details about failed requests, like missing resources or invalid mappings.
  • Test a static resource first: If you have an index.html in your webapp folder, try accessing localhost:8080/smartHouse/index.html. If this returns 404, the issue is with the app deployment itself, not the servlet. If it works, focus back on the servlet mapping.

One final tip: Since you followed the Oracle tutorial, cross-reference your configs line-by-line with the tutorial’s sample code. Sometimes a tiny typo or missing XML tag is the culprit!

内容的提问来源于stack exchange,提问作者Greg Sakaszwili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:12:10