2013-06-22 27 views
10

我使用Tomcat来为我的Java Servlets服务,这对我来说更是如此。我只需要提供服务,单独的Servlet请求,没有静态内容,既没有JSP等,所以我正在寻找一个可以嵌入到我的应用程序中的Servlet容器。我觉得如果剥离Jetty并单独使用它作为Servlet容器,它可以更具可扩展性并占用很小的内存空间,[我不需要Jetty的'Web Server'和其他部件]。所以我有几个问题,但是,将码头作为Servlet容器嵌入

  1. 如何在我的应用程序代码中嵌入Jetty以单独提供Servlet请求?
  2. 如果我在应用程序代码中嵌入了Jetty代码,我能否轻松升级Jetty版本?
  3. 我得到了码头代码在这里,如果我有嵌入Jetty的Servlet容器在我的应用程序,其中一个我应该从源头上使用, http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/snapshot/jetty-9.0.3.v20130506.tar.bz2, 码头-9.0.3.v20130506 /码头,servlet或码头 - 9.0 .3.v20130506/jetty-servlets

我打算为我的应用程序提供API请求,我正在寻找性能和可伸缩性作为主要限制条件。当然还有Servlet 3.0的支持。

回答

16

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

有大量示例显示如何将实现目标所需的各个部分连接在一起。

查看embedded examples in the jetty source tree

为了记录,jetty standalone实际上只是嵌入了一些启动和类路径相关引导的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(); 
    } 
} 
+1

使用jetty 9和servlet api 3.1的更新版本:https://github.com/jetty-project/embedded-servlet-3.1 – Kapep