嵌入式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:
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 yoursmartHouseapp 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).
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 yourMainclass 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.classfile lives in the project (no mismatched package names!).
Your pom.xml controls how your WAR is built—small mistakes here can break deployment:
- Ensure the
<packaging>tag is set towar(notjar). Tomcat can’t deploy JAR files as web apps. - Check the
<finalName>in the<build>section:
This name sets Tomcat’s default context path. If it’s set to something like<build> <finalName>smartHouse</finalName> </build>smartHouse-v1, your URL would need to belocalhost:8080/smartHouse-v1instead. - 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>
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 packageto rebuild your WAR, then unzip the file intarget/to verify:- The
WEB-INF/classes/folder contains your compiledMain.class(in the correct package hierarchy). - The
WEB-INF/web.xml(if used) has the exact servlet mappings you defined.
- The
- If deploying from an IDE (IntelliJ/Eclipse), make sure it’s configured to deploy the full WAR artifact, not a partial build.
Logs are your best friend for debugging 404s:
- Check Tomcat’s
logs/directory (usually in your Tomcat installation folder). Look atlocalhost.logandlocalhost_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.htmlin yourwebappfolder, try accessinglocalhost: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

