Version: 9.4.12.v20180830 |

Serving a WebApp from a Particular Port/Connector
Previous | Chapter 5. Configuring Contexts Home | Next

##### 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 for sponsored feature development

## Serving a WebApp from a Particular Port/Connector

Sometimes it is required to serve different web applications from different ports/connectors. The simplest way to do this is to create multiple `Server` instances. However, if contexts need to share resources (eg data sources, authentication), or if the mapping of ports to web applications is not cleanly divided, then the named connector mechanism can be used.

### Creating Multiple Server Instances

Creating multiple server instances is a straight forward process that includes embedding Jetty code by creating multiples instances of the Server class and configuring them as needed. This is also easy to achieve if you are configuring Jetty servers via XML. The `id` field in the Configure element of `jetty.xml` files is used to identify the instance that the configuration applies to, so to run two instances of the Server, you can copy the `jetty.xml`, jetty-http.xml and other jetty configuration files used and change the "Server" id to a new name. This can be done in the same style and layout as the existing `jetty.xml` files or the multiple XML files may be combined to a single file.

When creating new configurations for alternative server:

- Change all `id="Server"` to the new server name:

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

- For all connectors for the new server change the `refid` in the server argument:

    <Arg name="server"><Ref refid="OtherServer" /></Arg>

- Make sure that any references to properties like `jetty.http.port` are either renamed or replaced with absolute values.
- Make sure that any deployers `AppProviders` refer to a different "webapps" directory so that a different set of applications are deployed.

#### Example Other Server XML

The following example creates another server instance and configures it with a connector and deployer:

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

    <Configure id="OtherServer" class="org.eclipse.jetty.server.Server">
        <Set name="handler">
          <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
             <Array type="org.eclipse.jetty.server.Handler">
               <Item>
                 <New id="OtherContexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
               </Item>
               <Item>
                 <New class="org.eclipse.jetty.server.handler.DefaultHandler"/>
               </Item>
             </Array>
            </Set>
          </New>
        </Set>

      <Call name="addConnector">
        <Arg>
          <New class="org.eclipse.jetty.server.ServerConnector">
            <Arg name="server"><Ref refid="OtherServer" /></Arg>
            <Set name="port">8888</Set>
          </New>
        </Arg>
      </Call>

      <Call name="addBean">
        <Arg>
          <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
            <Set name="contexts">
              <Ref refid="OtherContexts" />
            </Set>

            <Call id="webappprovider" name="addAppProvider">
              <Arg>
                <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
                  <Set name="monitoredDirName"><Property name="jetty.base" default="." />/other-webapps</Set>
                  <Set name="defaultsDescriptor"><Property name="jetty.home" default="." />/etc/webdefault.xml</Set>
                  <Set name="extractWars">true</Set>
                  <Set name="configurationManager">
                    <New class="org.eclipse.jetty.deploy.PropertiesConfigurationManager"/>
                  </Set>
                </New>
              </Arg>
            </Call>
          </New>
        </Arg>
      </Call>
    </Configure>

To run the other server, add the extra configuration file(s) to the command line:

    java -jar start.jar jetty-otherserver.xml

### Named Connectors

It is also possible to use an extension to the virtual host mechanism with named to connectors to make some web applications only accessible by specific connectors. If a connector has a name "MyConnector" set using the `setName` method, then this can be referenced with the special virtual host name "@MyConnector".

Previous | Top | Next
Temporary Directories | Home | Creating Custom Error Pages

See an error or something missing? Contribute to this documentation at Github!(Generated: 2018-08-30)