java 将 Jetty 嵌入为 Servlet 容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17246512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:26:53  来源:igfitidea点击:

Embedding Jetty as a Servlet Container

javaservletsembedded-jetty

提问by Manikandaraj Srinivasan

I'm using Tomcat to serve my Java Servlets and it's kinda more for me. I just need to serve, Servlet Requests alone, no static content, neither JSP, etc. So I was looking for a Servlet container that can be embedded in my Application. I felt it if stripped Jetty and use it as a Servlet Container alone, it can be more scalable and occupying small memory footprint, [I don't need Jetty's 'Web Server' and other Parts]. So I've a few questions though,

我正在使用 Tomcat 来为我的 Java Servlet 提供服务,这对我来说更胜一筹。我只需要提供服务,单独的 Servlet 请求,没有静态内容,也没有 JSP 等。所以我正在寻找一个可以嵌入到我的应用程序中的 Servlet 容器。感觉如果把Jetty剥离出来,单独作为一个Servlet Container使用,可以扩展性更强,占用内存小,【不需要Jetty的'Web Server'等Parts】。所以我有几个问题,

  1. How do I embed Jetty in my Application Code to serve Servlet Requests alone?
  2. If I embed Jetty code in my Application Code, will I be able to easily upgrade Jetty Versions?
  3. I got the Jetty code here, if I have to embed Jetty's Servlet Container in my App, which one should I use from the source, http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/snapshot/jetty-9.0.3.v20130506.tar.bz2, jetty-9.0.3.v20130506/jetty-servlet or jetty-9.0.3.v20130506/jetty-servlets
  1. 如何在我的应用程序代码中嵌入 Jetty 以单独处理 Servlet 请求?
  2. 如果我在我的应用程序代码中嵌入 Jetty 代码,我是否能够轻松升级 Jetty 版本?
  3. 我在这里得到了 Jetty 代码,如果我必须在我的应用程序中嵌入 Jetty 的 Servlet 容器,我应该使用源代码http://git.eclipse.org/c/jetty/org.eclipse.jetty.project 中的哪一个 。 git/snapshot/jetty-9.0.3.v20130506.tar.bz2、jetty-9.0.3.v20130506/jetty-servlet 或 jetty-9.0.3.v20130506/jetty-servlets

I intend to serve API Requests with my Applications and I'm looking for Performance and Scalability as main constraints. And of course Servlet 3.0 support.

我打算在我的应用程序中提供 API 请求,并且我正在寻找性能和可扩展性作为主要约束条件。当然还有 Servlet 3.0 支持。

回答by Joakim Erdfelt

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

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

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

有大量示例可以展示如何将实现目标所需的各个部分联系在一起。

Check out the embedded examples in the jetty source tree.

查看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.

根据记录,独立码头实际上只是嵌入了一些启动和类路径相关引导程序的码头。它是相同的代码,并且以基本相同的方式组装。

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,对 JSP 不感兴趣,这很容易设置。(JSP 设置起来比较棘手,但可能)。

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

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

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

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();
    }
}