Jetty/Howto/SetContextPathto /
Contents
Introduction
To set your context path to / (the root context path), you need to use a Context deployment.
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 the mywebapp.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.
- Make sure your ContextProvider-based deployments are enabled in the start.ini. That is, make sure that etc/jetty-context.xml is present.
- Create a ${jetty.home}/contexts/mywebapp.xml that declares the <Set name="contextPath">/</Set> option.
- 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.
- 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-0.0.0.0-8080-test.war-_-any-/webapp/},/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-0.0.0.0-8080-test.war-_-any-/webapp/
- It was using the web application specified in /home/joakim/code/jetty/distros/jetty-distribution-7.6.0.RC3/webapps/test.war.