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

Jetty中WebAppContext结合web.xml配置Servlet遇404问题求助

Hey there! Let's break down why your web.xml-based Servlet setup is throwing a 404 error while the programmatic approach works. Since your code uses Jetty's WebAppContext, I’ll focus on Jetty-specific behaviors that often cause this mismatch.

Common Issues & Fixes

1. Double-check web.xml Location & Structure

First off, Jetty won’t recognize your web.xml unless it’s in the correct path: it must live inside a WEB-INF directory at the root of your web application. For Maven/Gradle projects, this means placing it at src/main/webapp/WEB-INF/web.xml. If the path is wrong, Jetty will ignore the file entirely.

Also, ensure your web.xml follows the proper Servlet spec structure. For Servlet 3.1+ (the most common modern version), use this header:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

Verify your Servlet mapping is accurate too—no typos in the fully qualified class name or URL pattern:

<servlet>
    <servlet-name>MyCustomServlet</servlet-name>
    <servlet-class>com.yourpackage.MyCustomServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyCustomServlet</servlet-name>
    <url-pattern>/my-servlet/*</url-pattern>
</servlet-mapping>

2. Fix Your WebAppContext Configuration

Your Launcher code needs to properly point Jetty to your web application root (where the WEB-INF folder lives). If this is missing or incorrect, Jetty won’t load the web.xml. Here’s a corrected example:

public class Launcher {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        
        WebAppContext webApp = new WebAppContext();
        // Point to your web app root (contains WEB-INF)
        webApp.setResourceBase("src/main/webapp");
        // Set root context path so your URL patterns work as expected
        webApp.setContextPath("/");
        
        server.setHandler(webApp);
        server.start();
        server.join();
    }
}

If you’re using a packaged WAR file, replace setResourceBase with setWar("target/your-app.war") instead.

3. Check for Case Sensitivity & Typos

On Linux/macOS, file paths and class names are case-sensitive. A tiny mistake like writing myServlet instead of MyServlet in web.xml will break the mapping. Double-check:

  • Fully qualified class names in <servlet-class>
  • <servlet-name> matches exactly between the servlet definition and mapping
  • URL patterns start with a / (e.g., /my-servlet instead of my-servlet)

4. Enable Jetty Debug Logs

If you’re still stuck, turn on debug logging for Jetty. This will show you exactly whether Jetty is loading your web.xml, registering your Servlet, or hitting any errors during startup. Add a logging framework like SLF4J + Logback, set the log level to DEBUG, and look for lines like:

  • Loaded web.xml from ... (confirms the file was found)
  • Registered servlet ... with mapping ... (confirms the Servlet was mapped correctly)

Example Working Setup

Here’s a complete, minimal example to compare against:

Launcher.java

public class Launcher {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        
        WebAppContext webApp = new WebAppContext();
        webApp.setResourceBase("src/main/webapp");
        webApp.setContextPath("/");
        
        server.setHandler(webApp);
        server.start();
        server.join();
    }
}

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <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>

Start with verifying the web.xml location and WebAppContext resource base—those are the most frequent culprits. If that doesn’t resolve the 404, debug logs will give you concrete clues about what’s failing during startup.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:33:56