Jetty Logo
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

DRAFT

This page contains content that we have migrated from Jetty 7 or Jetty 8 documentation into the correct format, but we have not yet audited it for technical accuracy in with Jetty 9. Be aware that examples or information contained on this page may be incorrect. Please check back soon as we continue improving the documentation, or submit corrections yourself to this page through Github. Thank you.

Serving a WebApp from a Particular Port

Creating Server Instances
Creating Context Files
An Alternative

You might need to serve webapp A only from port A and webapp B only from port B. The most efficient way to do this is with two org.eclipse.jetty.server.Server instances. There is also another, slightly less efficient An Alternative.

Server instance A has a connector listening on port A with webapp A defined, and Server instance B has webapp B and a connector listening on port B defined. For example:

You want to serve webapp A from port 8080, and webapp B from an SSL connector on port 8443. This involves setting up two Server instances and two context files.

Creating Server Instances

You set up two Server instances, each in its own jetty.xml file.

Creating jettyA.xml

The Server instance example for JettyA follows:

<Configure id="ServerA" class="org.eclipse.jetty.server.Server">

    <!-- set up the port for ServerA -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">10</Set>
          </New>
        </Item>
      </Array>
    </Set>

   <!-- set up a context provider for Server A -->
    <Call name="addLifeCycle">
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref refid="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsA</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>

</Configure>

Creating jettyB.xml

The Server instance example for JettyB follows:

<source lang=xml>
<Configure id="ServerB" class="org.eclipse.jetty.server.Server">

    <!-- set up the port for ServerB -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.ssl.SslSocketConnector">
            <Set name="Port">8443</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Keystore"><SystemProperty name="jetty.home" default="." />/etc/keystore</Set>
            <Set name="Password">OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4</Set>
            <Set name="KeyPassword">OBF:1u2u1wml1z7s1z7a1wnl1u2g</Set>
          </New>
        </Item>
      </Array>
    </Set>

     <!-- set up a context provider for ServerB -->
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref refid="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsB</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>

</Configure>

Creating Context Files

Now you need to set up two context files, one to describe the webapp you want to deploy on Server A and the other for the webapp to deploy on Server B. You then put these files into $JETTY_HOME/contextsA or $JETTY_HOME/contextsB respectively.

Creating ContextA

The example contextA.xml file follows:

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/A</Set>
      <Set name="contextPath">/webappA</Set>
       ...
    </Configure>

Creating ContextB

The example contextB.xml file follows:

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/B</Set>
      <Set name="contextPath">/webappB</Set>
       ...
    </Configure>

Running ContextA and ContextB

You run both Server instances in the same JVM by providing them both on the runline:

java -jar start.jar jettyA.xml jettyB.xml

Of course, you could also start two separate Jetty instances, one with jettyA.xml and the other with jettyB.xml. However, it is usually more efficient to run both Servers in the same JVM.

An Alternative

There is also an alternative way to achieve the same result as above, however it is slightly less efficient. It involves setting the list of connectors on a webapp from which it accepts requests. This is a less efficient solution than the one described above because the request is presented to each webapp, which then must decide to accept it or not. In the first solution, only one webapp is ever passed the request. In this alternate configuration, you only need a single Server instance. You define each of the connectors with a unique name, and then assign each webapp a list of connector names to which it should respond.

This alternative requires a jetty.xml file and two context files.

Creating a jetty.xml File

The example jetty.xml follows:

<Configure class="org.eclipse.jetty.server.Server">

    <!-- set up both connectors -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New  class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>
            <Set name="name">connA</Set>
          </New>
        </Item>
        <Item>
          <New id="connB" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">9090</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>            
            <Set name="name">connB</Set>
          </New>
        </Item>
      </Array>
    </Set>

     <!-- set up a context provider -->
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref refid="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>
</Configure>

Creating ContextA

The example contexta.xml follows:

    <Configure  class="org.eclipse.jetty.webapp.WebAppContext">      
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/A</Set>
      <Set name="contextPath">/webappA</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connA</Item>
        </Array>
       </Set>
      ...
    </Configure>

Creating ContextB

The example contextb.xml follows:

    <Configure  class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/B</Set>
      <Set name="contextPath"/webappB</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connB</Item>
        </Array>
      </Set>
    </Configure>

Running

Now start jetty as usual (if your Server config file is called jetty.xml you can omit it from the run line):

java -jar start.jar

See an error or something missing? Contribute to this documentation at Github!