Jetty:部署到Jetty

Web应用的框架

标准Jetty公布版本号能部署标准servlet Spec Web应用和Jetty内部ContextHandler部署描写叙述符,或者两者的一个混合。
Web应用是可部署的动态(servlets、filters、jsps、等等)和静态内容、支持库、和绑定到特定上下文路径的描写性的元数据的集合。
格式和布局终于都是通过Servlet Spec定义。你能够查阅官方Servlet Spec文档获取关于Web应用布局和结构的很多其它细节,这里将给出一个主要的轮廓。
Web应用能被捆绑到一个单个的Web文件(WAR文件)或者作为一个目录树:
 1)/WEB-INF/
 专门的Servlet API定义目录,通常存储和Web应用相关但不为外部訪问的不论什么东西。
 假设你有内容被你的Web应用内部訪问,但不会被web浏览器直接地訪问。你就应该把他们放在这里。
 2)/WEB-INF/web.xml
 必须的部署描写叙述符,用于定义你的Web应用的各种行为。


 3)/WEB-INF/classes/
 Web应用的java classes文件放置文件夹。
 4)/WEB-INF/lib/
 JAR文件放置的文件夹。

自己主动的Web应用部署

为了部署Web应用,最简单的方式就是放置你的WAR文件或者解压后的WAR目录到${jetty.home}/webapps/。Jetty的部署扫描器将发现它,并部署它到同名的Context路径下。
上下文路径基于你的WAR的文件名称(或目录名)。

以下是一些样例(文件或目录名->上下文路径):
/webapps/footrope.war -> http://host/footrope/
/webapps/baggywrinkle-1.0.war -> http://host/baggywrinkle-1.0/
/webapps/lazaret-2.1.3-SNAPSHOT.war -> http://host/lazaret-2.1.3-SNAPSHOT/
/webapps/belaying-pins/WEB-INF/web.xml -> http://host/belaying-pins/
/webapps/root.war (special name) -> http://host/
/webapps/root/WEB-INF/web.xml (special name) -> http://host/

配置特定的Web应用部署

用自己主动的Web应用部署方式是高速和简单的。但有时你或许须要调整一些部署属性(比如,你想指定一个上下文路径而不是使用文件名称,或者你想为这个Web应用指定一个特定的数据库连接池),你能够使用Jetty Deployable Descriptor XML文件来实现。
Jetty支持通过XML文件部署Web应用,这些XML文件将构建一个Jetty能部署的ContextHandler实例。

使用主要的描写叙述符文件

在默认的Jetty安装中,Jetty扫描他的$JETTY_HOME/webapps目录查找上下文部署描写叙述符文件。为了用这个文件部署一个web应用。仅仅须要放置这个文件在那个目录中。
部署描写叙述符文件自己是一个配置了一个WebAppContext类的XML文件。

为了一个主要的安装。你须要设置仅两个属性:
     war:web应用文件(或目录)的文件路径;
     contextPath:web应用使用的上下文路径。


比如。以下是是一个描写叙述符文件,将/opt/myapp/myapp.war部署到上下文路径/wiki:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/wiki</Set>
  <Set name="war">/opt/myapp/myapp.war</Set>
</Configure>

或者你能够使用SystemProperty和Property元素在你的描写叙述符文件里,假设你设置了系统属性myapp.home=/opt/myapp。你能重写上面的样例:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/wiki</Set>
  <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>
</Configure>

假设你须要为你的应用改动home路径。你仅须要改变系统属性。这在有些场合很实用。

配置高级描写叙述符文件

假设你看WebAppContext类的文档,你会发现它有很多属性而不是唯独上面提到的两项,以下是一些为你的描写叙述符文件配置高级属性的样例。


第一个样例告诉Jetty在部署WAR文件的时候不要解压。

这能帮助用户避免改动解压后的WAR,导致web应用下次部署的时候改动被还原。

<?

xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/wiki</Set> <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set> <Set name="extractWAR">false</Set> </Configure>

以下的样例是获取JavaEE Servlet上下文,并为它设置初始化參数。你也能使用setAttribute方法设置Servlet上下文属性。然而,因为web应用的web.xml在部署描写叙述符之后处理。web.xml值可能覆盖你设置的属性值。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/wiki</Set>
  <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>
  <Get name="ServletContext">
     <Call name="setInitParameter">
       <Arg>myapp.config</Arg>
       <Arg><SystemProperty name="myapp.home">/config/app-config.xml</Arg>
    </Call>
  </Get>
</Configure>

以下是设置一个指定的web.xml的覆盖描写叙述符的样例,该描写叙述符在web应用的web.xml之后处理,因此它能够覆盖web.xml中设置的同名属性。

当你想加入參数或者额外的附加Servlet mappings又不想改动压缩的WAR文件的时候。能够採用这样的方式。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/wiki</Set>
  <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>
  <Set name="overrideDescriptor">/opt/myapp/overlay-web.xml</Set>
</Configure>

以下的样例不仅配置web应用上下文,并且配置一个数据库连接池。

<?

xml version="1.0" encoding="ISO-8859-1"?

> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/wiki</Set> <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set> </Configure> <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <Arg>jdbc/DSTest</Arg> <Arg> <New class="org.apache.commons.dbcp.BasicDataSource"> <Set name="driverClassName">org.some.Driver</Set> <Set name="url">jdbc.url</Set> <Set name="username">jdbc.user</Set> <Set name="password">jdbc.pass</Set> </New> </Arg> </New>

WebAppContext还有很多其他的设置项,详细能够參看WebAppContext的javadoc文档。

部署WebAppContexts的处理

web应用在进入service之前。须要一些处理:解压、为它们的jar指定classloader、web.xml和web-fragment.xml描写叙述符处理、类的凝视扫描。当web应用变得更加复杂时,我们添加方法帮助你在部署时加入或者降低处理的数量。在这节我们将学习部署处理和你怎么裁剪它。

Jetty的配置类

当一个webapp正在被部署时,一系列org.eclipse.jetty.webapp.Configuration类将被应用到它。它们中的每个都履行一个特定的功能。这些配置的顺序是有意义的。兴许的配置在先前的配置基础上处理。

以下是被应用到每个org.eclipse.jetty.webapp.WebAppContext的配置列表。按顺序:

org.eclipse.jetty.webapp.WebInfConfiguration -> 抽取war,整理jars和定义classpath
org.eclipse.jetty.webapp.WebXmlConfiguration -> 处理WEB-INF/web.xml文件
org.eclipse.jetty.webapp.MetaInfConfiguration -> 为META-INF/resources和META-INF/web-fragment.xml查看container和webapp jars
org.eclipse.jetty.webapp.FragmentConfiguration -> 处理全部的发现的META-INF/web-fragment.xml文件
org.eclipse.jetty.webapp.JettyWebXmlConfiguration -> 处理WEB-INF/jetty-web.xml文件

一个配置类的结构

一个配置类在WebAppContext的生命周期中有五个阶段:
 1)preConfigure:在WebAppContext启动时运行。配置应该发现它在兴许的阶段须要的全部资源;
 2)configure:这个阶段将运行配置类的工作。一般会使用在preConfigure阶段发现的资源;
 3)postConfigure:这个阶段配置将清理上一个阶段创建的一些资源,这些资源在WebAppContext的生命周期中将不再须要;
 4)deconfigure:这个阶段在WebAppContext被停止时出现,同意配置撤销它创建的资源/元数据。
 5)destroy:这个阶段在WebAppContext从service移除时调用,比如:和它相关的war文件将从$JETTY_HOME/webapps目录中删除。
每一个配置类的每一个阶段都按照配置类的配置顺序被调用,比如,用默认配置类作为样例。preConfigure()被调用的顺序按照WebInfConfiguration、WebXmlConfiguration、MetaInfConfiguration、FragmentConfiguration、最后JettyWebXmlConfiguration,这个循环又開始在configure()阶段,然后是postConfigure()阶段,循环再次反复在deconfigure(),终于在destroy()阶段。

通过创建额外的配置扩展容器支持

由上所述,有一个默认的配置集合支持主要的webapp部署。你将注意到我们没有提到一些JavaEE特征,比如JNDI,也没有提到高级servlet spec特征。比如凝视。那是由于Jetty的哲学是同意用户依照自己的须要裁剪容器。假设你不须要这些特征,那么你不须要为它们付出代价 - 一个重要的考虑是由于这些特征比如凝视须要大量的和耗时的WEB-INF/lib包的扫描,这些都可能成为部署延迟的源头。我们将在以下的“其他配置”中看到还有一个Jetty提供的webapp工具。能够帮助削减分析jar包的时间消耗。


Jetty採用插件的方式提供对JNDI和凝视的支持。

首先,让我们看看怎么支持JNDI。


我们须要用到两个额外的配置:

org.eclipse.jetty.plus.webapp.EnvConfiguration -> 创建java:comp/env为webapp。应用WEB-INF/jetty-env.xml文件
org.eclipse.jetty.plus.webapp.PlusConfiguration -> 处理JNDI关联的WEB-INF/web.xml标记,而且与名目挂钩

这些配置必须依照上面的顺序加入。而且应该在配置列表中的org.eclipse.jetty.webapp.JettyWebXmlConfiguration前被插入。支持JNDI的细节将在后面用一个单独的章节解说。

以下来看看怎么凝视。我们须要仅一个额外的配置类帮助提供servlet凝视扫描:

org.eclipse.jetty.annotations.AnnotationConfiguration -> 扫描容器和web app jars查找@WebServlet、@WebFilter、@WebListener等

上面的配置必须在org.eclipse.jetty.webapp.JettyWebXmlConfiguration之前插入。

凝视的配置细节也将在后面用一个单独的章节解说。

怎么设置配置列表

你能够为Jetty指定一个不同的配置列表,通过以下的方式。


 1)直接在WebAppContext中设置配置列表
假设你仅希望影响一个webapp。这可能是最easy的方式。你能够通过上下文配置文件或者代码来设置配置列表,以下是一个样例怎么为JNDI和凝视加入配置:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/my-cool-webapp</Set>
 
  <Set name="configurationClasses">
    <Array type="java.lang.String">
      <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
      <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
    </Array>
  </Set>
 
</Configure>

 2)通过部署器为全部的webapp设置配置列表
假设你用部署器(deployer)。你能在WebAppProvider设置配置列表。它们将被应用到每个被部署器部署的WebAppContext。

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
  <Call name="addBean">
    <Arg>
      <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
        <Set name="contexts">
          <Ref refid="Contexts" />
        </Set>
        <Call id="webappprovider" name="addAppProvider">
          <Arg>
            <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
              <Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps</Set>
              <Set name="configurationClasses">
                <Array type="java.lang.String">
                  <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
                  <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
                  <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
                  <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
                </Array>
              </Set>
            </New>
          </Arg>
        </Call>
      </New>
    </Arg>
  </Call>
</Configure>

3)加入或者插入到一个存在的列表
你能够将配置类加入或者插入一个已经存在的配置列表,以下是一个样例。它实现了为JNDI加入一个配置支持 - 你能够通过xml配置文件按,或者通过代码,这里使用配置文件。实际上它是Jetty公布版本号的$JETTY_HOME/etc/jetty-plus.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
  <!-- =========================================================== -->
  <!-- Add plus Configuring classes to all webapps for this Server -->
  <!-- =========================================================== -->
  <Call class="org.eclipse.jetty.webapp.Configuration$ClassList" name="setServerDefault">
    <Arg><Ref refid="Server" /></Arg>
    <Call name="addAfter">
      <Arg name="afterClass">org.eclipse.jetty.webapp.FragmentConfiguration</Arg>
      <Arg>
        <Array type="String">
          <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
        </Array>
      </Arg>
    </Call>
  </Call>
 
</Configure>

org.eclipse.jetty.webapp.Configuration.ClassList类提供了以下的方法:
 1)addAfter:在给出的配置类名的后面插入提供的配置类列表;
 2)addBefore:在给出的配置类名之前插入提供的配置类列表。

其他配置

org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern

这是一个上下文属性,用于设置在an org.eclipse.jetty.webapp.WebAppContext。控制容器的c

原文地址:https://www.cnblogs.com/lxjshuju/p/7072473.html