Version: 9.4.21.v20190926 |

Configuring a Specific Web Application Deployment
Previous | Chapter 4. Deploying to Jetty 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

## Configuring a Specific Web Application Deployment

Using the Automatic Web Application Deployment model is quick and easy, but sometimes you might need to tune certain deployment properties (for example, you want to deploy with a context path that is not based on the file name, or you want to define a special database connection pool just for this web application). You can use a Jetty Deployable Descriptor XML File to accomplish such tuning.

### Jetty Deployable Descriptor XML File

Jetty supports deploying Web Applications via XML files which will build an instance of a ContextHandler that Jetty can then deploy.

### Using Basic Descriptor Files

In a default Jetty installation, Jetty scans its `$JETTY_HOME/webapps` directory for context deployment descriptor files. To deploy a web application using such a file, simply place the file in that directory.

The deployment descriptor file itself is an xml file that configures a `WebAppContext` class. For a basic installation only two properties need configured:

war
  The filesystem path to the web application file (or directory)
contextPath
  The context path to use for the web application

For example, here is a descriptor file that deploys the file `/opt/myapp/myapp.war` to the context path `/wiki`:

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

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/wiki</Set>
      <Set name="war">/opt/myapp/myapp.war</Set>
    </Configure>

Both `SystemProperty` and `Property` elements can be used in the descriptor file. For example, if the system property is set to `myapp.home=/opt/myapp`, the previous example can be rewritten as:

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

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/wiki</Set>
      <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>
    </Configure>

If the home path for an application needs altered, only the system property needs changed. This is useful if the version of an app is frequently changed.

> ### Note

> To ensure your `web.xml` files are validated, you will need to set the `validateXml` attribute to true as described here.

### Configuring Advanced Descriptor Files

Official documentation for the for the `WebAppContext` class lists all the properties that can be set. Here are some examples that configure advanced options in the descriptor file.

This first example tells Jetty not to expand the WAR file when deploying it. This can help make it clear that users should not make changes to the temporary unpacked WAR because such changes do not persist, and therefore do not apply the next time the web application deploys.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.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>

The next example retrieves the JavaEE Servlet context and sets an initialization parameter on it. The `setAttribute` method can also be used to set a Servlet context attribute. However, since the `web.xml` for the web application is processed after the deployment descriptor, the `web.xml` values overwrite identically named attributes from the deployment descriptor.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.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>

The following example sets a special `web.xml` override descriptor. This descriptor is processed after the web application’s `web.xml`, so it may override identically named attributes. This feature is useful when adding parameters or additional Servlet mappings without breaking open a packed WAR file.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.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>

The next example configures not only the web application context, but also a database connection pool (see Datasource Examples) that the application can then use. If the `web.xml` does not include a reference to this data source, an override descriptor mechanism (as shown in the previous example) can be used to include it.

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

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="contextPath">/wiki</Set>
      <Set name="war"><SystemProperty name="myapp.home"/>/myapp.war</Set>

      <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>
    </Configure>

There are many other settings that can be changed in a `WebAppContext`. The javadoc for `WebAppContext` is a good source of information. Also see the documentation on avoiding zip file exceptions for a description of `WebAppContext` settings that determine such things as whether or not the war is automatically unpacked during deployment, or whether certain sections of a webapp are copied to a temporary location.

Previous | Top | Next
Automatic Web Application Deployment | Home | Deployment Processing of WebAppContexts

See an error or something missing? Contribute to this documentation at Github!(Generated: 2019-09-26)