依据Eclipse Jetty Maven教程构建的简易应用返回404问题咨询
Hey there, let's work through that 404 error you're hitting with your Jetty + Maven app. Based on what you've shared, here are the key things to check and fix:
1. Fix Servlet Mapping Configuration
Your SwbppServlet extends HttpServlet, but it looks like you might be missing the mapping that tells Jetty which URLs should route to this servlet. Here are two common ways to set this up:
Option 1: Use Annotations (Servlet 3.0+)
Add the @WebServlet annotation directly to your servlet class to define its access path:
package org.firezeal; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/swbpp") // This defines the URL path for your servlet public class SwbppServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello from SwbppServlet!"); } }
Option 2: Use web.xml (Traditional Approach)
If you prefer XML configuration, create a web.xml file at src/main/webapp/WEB-INF/web.xml with the following mapping:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="4.0"> <servlet> <servlet-name>SwbppServlet</servlet-name> <servlet-class>org.firezeal.SwbppServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SwbppServlet</servlet-name> <url-pattern>/swbpp</url-pattern> </servlet-mapping> </web-app>
2. Validate Maven Jetty Plugin Setup
Make sure your pom.xml has the Jetty plugin configured correctly, including the application's context path. Here's a standard setup:
<build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.x</version> <!-- Match your Jetty documentation version --> <configuration> <webApp> <contextPath>/swbpp</contextPath> <!-- Root path for your app --> </webApp> <scanIntervalSeconds>10</scanIntervalSeconds> <!-- Auto-reload on changes --> </configuration> </plugin> </plugins> </build>
After configuring this, start Jetty with mvn jetty:run and double-check your access URL. For example, if your context path is /swbpp and servlet mapping is /swbpp, the full URL would be http://localhost:8080/swbpp/swbpp.
3. Confirm Maven Web Project Structure
Ensure your project follows the standard Maven web app structure—missing directories can cause Jetty to fail recognizing your app as a web project:
- ✅
src/main/java/org/firezeal/SwbppServlet.java(You already have this correct) - ✅
src/main/webapp/(Root for web resources) - ✅
src/main/webapp/WEB-INF/(Holdsweb.xmland other configs)
If src/main/webapp is missing, create it immediately—Jetty relies on this directory to deploy web apps.
4. Check if Your Servlet is Compiled and Deployed
- Run
mvn compileto verify there are no compilation errors. - Check the
target/classes/org/firezeal/directory to confirmSwbppServlet.classexists. - When starting Jetty with
mvn jetty:run, look for console logs indicating the servlet was loaded. You should see lines like:INFO: Initializing Servlet 'SwbppServlet'
If you don't see this, your servlet isn't being deployed properly.
5. Double-Check Your Access URL
Most 404 errors boil down to a typo in the URL. Let's clarify:
- If Jetty uses the default port (8080), your context path is
/myapp, and servlet mapping is/hello, the correct URL ishttp://localhost:8080/myapp/hello - Don't mix up the context path (from Jetty plugin config) and the servlet mapping (from annotation/web.xml)
内容的提问来源于stack exchange,提问作者Daniel Lynch

