java - 将 Jetty 作为 Servlet 容器嵌入

标签 java servlets embedded-jetty

我正在使用 Tomcat 来为我的 Java Servlet 提供服务,这对我来说有点多。我只需要服务,单独的 Servlet 请求,没有静态内容,也没有 JSP 等。所以我一直在寻找一个可以嵌入到我的应用程序中的 Servlet 容器。我觉得如果把Jetty剥离出来,单独作为一个Servlet Container使用,可以扩展性更好,占用内存小,[我不需要Jetty的'Web Server'和其他部分]。所以我有几个问题,

  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

我打算通过我的应用程序来处理 API 请求,并且我正在寻找性能和可扩展性作为主要约束。当然还有 Servlet 3.0 支持。

最佳答案

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

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

查看 embedded examples in the jetty source tree .

郑重声明,jetty standalone 实际上只是嵌入了一些与启动和类路径相关的 Bootstrap 的 jetty。它是相同的代码,并且以基本相同的方式组装。

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

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

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

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

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

关于java - 将 Jetty 作为 Servlet 容器嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246512/

相关文章:

java - 如何在 Netbeans 中使用 java 搜索 mongodb 数据库中的条目?

java - 尝试将 AndroidJUnit4 与标准 JUnit 运行器和委托(delegate)运行器一起使用 'org.robolectric.RobolectricTestRunner' 无法加载

java - JSP 中选择表单填充的问题

java - GenericFilterBean vs OncePerRequestFilter 何时分别使用?

java - Keycloak注销不会结束 session

java - 如何使用嵌入式 jetty 添加 servlet 过滤器

java - 你弄脏之前会打扫卫生吗?将清理代码放入 finally block 中

java - 忽略属性占位符 - Spring Boot

Tomcat 网络健康监控

java - Jetty 自定义错误处理程序类被调用但没有可抛出对象