【问题标题】:Using web.xml with WebAppContext in Jetty在 Jetty 中使用 web.xml 和 WebAppContext
【发布时间】:2018-09-11 14:44:46
【问题描述】:

这是我的第一个 Web 应用程序,我只是尝试按照指南并使用在 web.xml 指定的 servlets 启动我的服务器,但似乎我的操作不会改变服务器的功能结果是 404 错误。但是,如果我以编程方式指定 servlet 就可以了。谁能弄清楚这一切应该如何运作? 这是我的服务器代码

public class Launcher
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        WebAppContext web = new WebAppContext();
        web.setContextPath("/");
        web.setWar("src/main/web/WEB-INF/web.xml");
        //web.addServlet(MyServlet.class,"/"); This line works just fine
        server.setHandler(web);
        server.start();
        server.join();
    }
}

我的 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>MyServlet</servlet-name>
        <servlet-class>lab3.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>*</url-pattern>
    </servlet-mapping>
</web-app>

【问题讨论】:

    标签: servlets jetty web.xml


    【解决方案1】:

    WEB-INF/web.xml 是描述符。

    WebAppContext.setWar(String) 的调用是针对基本资源位置的。

    注意:基本资源位置是必需的。您必须将其设置为有效位置,以便ServletContext 可以正常工作。

    有用于设置基本资源位置的备用 API:您也可以使用 WebAppContext.setBaseResource(Resource)WebAppContext.setResourceBase(String),它们的含义相同。

    如果您想指定网络描述符位置,请使用WebAppContext.setDescriptor(String)

    对于您的代码示例,您似乎想要使用

        Server server = new Server(8080);
        WebAppContext web = new WebAppContext();
        web.setContextPath("/");
        web.setWar("src/main/web"); // TODO: resolve this to a an absolute path or URI!
        server.setHandler(web);
        server.start();
        server.join();
    

    关于该 TODO,请参阅jetty-project/embedded-jetty-cookbook 的示例,特别是...

    重要的是要认识到默认情况下类路径将基于基本资源位置。

    类将从${base.resource.uri}/classes查找。

    我指出这一点,因为我们在您的示例中看到了路径 src/main/web,我怀疑您正在尝试在 IDE 中创建一个实时(未构建)项目。这种设置需要您进行更多的手动工作,因为基础资源和类位于不同的位置。

    在这种情况下,您需要手动指定类的位置。

    又名。

        Server server = new Server(8080);
        WebAppContext web = new WebAppContext();
        web.setContextPath("/");
    
        Path baseResource = new File("src/main/web").toPath();
        Path classesDir = new File("target/thewebapp/WEB-INF/classes").toPath();
    
        if (!Files.exists(baseResource))
            throw new FileNotFoundException("Unable to find Base Resource Dir: " + baseResource);
        if (!Files.exists(classesDir))
            throw new FileNotFoundException("Unable to find Classes Dir: " + classesDir);
    
        web.setBaseResource(new PathResource(baseResource.toAbsolutePath()));
        web.setExtraClasspath(classesDir.toAbsolutePath().toString());
        server.setHandler(web);
        server.start();
        server.join();
    

    最后我想指出一些其他方法可以将 webapp 打包到 Embedded-jetty ...

    embedded-jetty-uber-jar

    https://github.com/jetty-project/embedded-jetty-uber-jar

    这使用ServletContextHandler手动构建一个webapp,没有使用WEB-INF/web.xml,没有字节码扫描,没有注释扫描,而且它的启动速度非常快。

    上述项目构建了一个 jar,其中包含运行 Web 应用所需的一切。

    嵌入式码头实战

    https://github.com/jetty-project/embedded-jetty-live-war

    这个项目有点复杂,它构建了 3 个部分(webapp、服务器、引导程序),然后将它们组装到一个单一的 war 文件中。

    这从一场简单的战争开始,然后通过按照您想要的方式预先配置的服务器来增强它。这个新的“live-war”在一个单独的子项目中组装成一个新的 war 文件。

    此war文件可以正常部署,并且它可以作为独立的自执行war文件直接运行,并带有完整的服务器实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 2019-11-10
      • 2020-08-19
      • 2019-06-08
      • 2011-11-05
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多