且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Jetty作为Servlet容器嵌入

更新时间:2023-12-03 13:42:16

您正在寻找的是在嵌入式场景中运行Jetty。

What you are looking for is running Jetty in an embedded scenario.

有很多例子可以说明如何将实现目标所需的各种部分联系在一起。

There's plenty of examples available showing how to tie together the various pieces you need to accomplish your goals.

查看嵌入式示例在码头源代码树中

为了记录,jetty standalone实际上只是一个嵌入了一些启动和类路径相关引导的jetty。它是相同的代码,并以基本相同的方式组装。

For the record, jetty standalone is really just jetty embedded with a few startup and classpath related bootstraps. It is the same code, and assembled in basically the same way.

既然你说你想要Servlet 3.0,对JSP没兴趣,这很容易设置。 (JSP设置比较棘手,但可能)。

Since you stated you want Servlet 3.0, have no interest in JSP, this is rather easy to setup. (JSP is trickier to setup, but possible).

对于servlet 3.0特定的嵌入,有一个完整的示例项目在github上托管。

For servlet 3.0 specific embedding, there's a complete example project hosted at github.

https://github.com/jetty-project/embedded-servlet-3.0

简而言之,您将拥有以下初始化代码。

In short, you'll have the following initialization code.

package com.company.foo;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.TagLibConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

public class EmbedMe {
    public static void main(String[] args) throws Exception {
        int port = 8080;
        Server server = new Server(port);

        String wardir = "target/sample-webapp-1-SNAPSHOT";

        WebAppContext context = new WebAppContext();
        // This can be your own project's jar file, but the contents should
        // conform to the WAR layout.
        context.setResourceBase(wardir);
        // A WEB-INF/web.xml is required for Servlet 3.0
        context.setDescriptor(wardir + "WEB-INF/web.xml");
        // Initialize the various configurations required to auto-wire up
        // the Servlet 3.0 annotations, descriptors, and fragments
        context.setConfigurations(new Configuration[] {
                            new AnnotationConfiguration(), 
                            new WebXmlConfiguration(),
                            new WebInfConfiguration(), 
                            new TagLibConfiguration(),
                            new PlusConfiguration(), 
                            new MetaInfConfiguration(),
                            new FragmentConfiguration(), 
                            new EnvConfiguration() });

        // Specify the context path that you want this webapp to show up as
        context.setContextPath("/");
        // Tell the classloader to use the "server" classpath over the
        // webapp classpath. (this is so that jars and libs in your
        // server classpath are used, requiring no WEB-INF/lib 
        // directory to exist)
        context.setParentLoaderPriority(true);
        // Add this webapp to the server
        server.setHandler(context);
        // Start the server thread
        server.start();
        // Wait for the server thread to stop (optional)
        server.join();
    }
}