在Jetty中将web.xml与WebAppContext一起使用 [英] Using web.xml with WebAppContext in Jetty

查看:985
本文介绍了在Jetty中将web.xml与WebAppContext一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个Web应用程序,我只是尝试遵循指南并使用在web.xml中指定的 servlet 启动服务器,但是看来我的操作并没有改变服务器的功能结果为 404 错误.但是,如果我以编程方式指定servlet,就可以了.谁能弄清楚这一切应该如何工作? 这是我的服务器代码

This is my first web application, I am just trying to follow guides and launch my server with servlets specified at web.xml, but it seems that my actions don't change functionality of the server and the result is 404 error. But if I programmatically specify servlets its OK. Could anyone figure it out how this all should work? Here is my code for server

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看起来像这样

And my web.xml looks like this

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

推荐答案

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

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

The call to WebAppContext.setWar(String) is for the base resource location.

注意:基本资源"位置是必需的.您必须将其设置在有效位置,以使ServletContext正常运行.

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

There are alternate APIs for setting the base resource location: you can use WebAppContext.setBaseResource(Resource) and WebAppContext.setResourceBase(String) as well, they mean the same thing.

如果要指定Web描述符的位置,请使用WebAppContext.setDescriptor(String).

If you want to specify the web descriptor location use WebAppContext.setDescriptor(String).

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

For your code example, it appears you want to use

    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中的示例,特别是...

About that TODO, see the examples at jetty-project/embedded-jetty-cookbook, specifically ...

  • WebAppContextFromFileSystem.java
  • WebAppContextFromClasspath.java

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

It's important to recognize that by default the classpath will be based off of the Base Resource location.

将从${base.resource.uri}/classes查找班级.

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

I point this out, because we see the path src/main/web in your example, I suspect you are trying to make a live (unbuilt) project in an IDE work. This kind of setup will require more manual work for you, as the base resource, and classes are in different locations.

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

In that case you'll need to specify where the classes are manually.

aka.

    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打包为嵌入式码头...

Finally I want to point out some other ways you can package a webapp up in embedded-jetty ...

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

这使用ServletContextHandler来手动构建一个Web应用程序,不使用WEB-INF/web.xml,不扫描字节码,不注释注释,并且其启动速度非常快.

This uses the ServletContextHandler to manually build up a webapp, no WEB-INF/web.xml is used, no bytecode scanning, no annotation scanning, and its blazingly fast to startup.

上面的项目使用运行Webapp所需的一切来构建一个jar.

The above project builds a single jar with everything you need to run your webapp.

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

这个项目稍微复杂一点,它会构建3个部分(webapp,服务器,引导程序),然后将它们组装成一个war文件.

This project is a bit more complex, it builds 3 parts (webapp, server, bootstrap) and then assembles them together into a single war file.

这从一个简单的战争开始,然后通过以您希望的方式预配置的服务器对其进行增强.这个新的实战"在一个单独的子项目中被组装为新的战争文件.

This starts with a simple war, but then enhances it with a server preconfigured the way you want it. This new "live-war" is assembled as a new war file in a separate sub-project.

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

This war file can be deployed as normal, and it can be run directly as a standalone self-executing war file, complete with a full server instance.

这篇关于在Jetty中将web.xml与WebAppContext一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆