Jump to: navigation, search

Jetty/Howto/SetContextPathto /

< Jetty‎ | Howto
Revision as of 17:04, 16 January 2012 by Boulay.intalio.com (Talk | contribs)


To set your context path to / you need to use a Context deployment.

Be aware
Java Servlet Specification 2.5 discourages an empty context path string, and Java Servlet Specification 3.0 effectively forbids it.

In Jetty 7.x, the AppProviders assigned to the DeploymentManager handle context path assignment.

By default on the Jetty Distribution, both the WebAppProvider and ContextProvider are enabled. This is important to know because it influences your decision on where to put themywebapp.war file.

Examine the ${jetty.home}/start.ini file, and you'll see that it contains both references to etc/jetty-webapps.xml and etc/jetty-contexts.xml.

Using the WebAppProvider

The WebAppProvider's role is to look in the ${jetty.home}/webapps/ directory for any deployable applications (such as *.war), and deploy them onto a context of the same name as the filename. For example, the WebAppProvider deploys ${jetty.home}/webapps/MyApp-2.4.war into the context /MyApp-2.4. There is also the special root.war reserved word that deploys into the context / . While this is the easiest deployment mechanism, it sacrifices control over deployment specifics.

Using the ContextProvider

The ContextProvider's role is to look in the ${jetty.home}/contexts/ directory for any jetty-xml formatted, deployable contexts. This deployment mechanism gives you the maximum control over the deployment, as this XML file can control anything that is ultimately resolved to a org.eclipse.jetty.server.handler.ContextHandler base class, of which WebAppContext (WARs, servlets, etc.) are part. The most common use is to specify a WebAppContext-based XML file, and control things such as what files and directories make up the web application, what temporary directory to use, and even what Context Path to use.

Setting the Context Path with the ContextProvider

To set the context path to / , follow these steps.

  1. Make sure your ContextProvider-based deployments are enabled in the start.ini. That is, make sure that etc/jetty-context.xml is present.
  2. Create a ${jetty.home}/contexts/mywebapp.xml that declares the <Set name="contextPath">/</Set> option.
  3. If you have the etc/jetty-webapps.xml present in your start.ini, do not put your mywebapp.war in ${jetty.home}/webapps because doing so causes the WebAppProvider to also deploy the same webapp, and confuses your deployment.
  4. Finally, you can see how the jetty distribution itself does this by examining ${jetty.home}/contexts/test.xml. Jetty loads the ${jetty.home}/webapps/test.war via the ContextProvider's use of the ${jetty.home}/contexts/test.xml into the / context path.

Studying the Logs

Look at the logs:

2012-01-13 13:56:28.779:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/tmp/jetty-},/home/joakim/code/jetty/distros/jetty-distribution-7.6.0.RC3/webapps/test.war

That reveals that WebAppContext was

  • Started on {/, (the root Context Path)
  • It was using the temp/work directory file:/tmp/jetty-
  • It was using the web application specified in /home/joakim/code/jetty/distros/jetty-distribution-7.6.0.RC3/webapps/test.war.