Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the root element of my Jetty webapp url?

I am running a Java webapp (let's call it mywebapp).

Currently I access my page in this webapp by pointing locally to:

http://localhost:9000/mywebapp/mystuff

However, I need to access it using:

http://localhost:9000/mystuff

How can I do this? I've tried messing with some confs, but to no avail...

This is my current root.xml:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="contextPath">/root</Set>
    <Set name="war">
        <SystemProperty name="app.webapps.path"/>/mywebapp.war
    </Set>
</Configure>

Also tried:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="contextPath">/</Set>
    <Set name="war">
        <SystemProperty name="app.webapps.path"/>/mywebapp.war
    </Set>
</Configure>

I'm using Maven - not sure if that may be making the difference.

Thanks!

like image 538
laura Avatar asked Jan 13 '12 16:01

laura


2 Answers

In order to set your context path to "/" you'll want to use a Context deployment. Note: You MUST specify a context path string for path spec mapping reasons. An empty context path string "" is only valid as a mapping result from requests to the context root. See Section 12.2 of the servlet spec)

In Jetty 7.x, the context path assignment is handled by the AppProviders assigned to the DeploymentManager.

By default, on the Jetty Distribution, both the WebAppProvider and ContextProvider are enabled. This is important to know later, as it will influence your decisions on where to put the mywebapp.war file.

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

WebAppProvider's role is to pay attention to 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. In other words ${jetty.home}/webapps/MyApp-2.4.war is deployed into the context "/MyApp-2.4". There is also the special "root.war" reserved word that will deploy into the context "/". While this is the easiest deployment mechanism, it sacrifices control over the deployment specifics.

ContextProvider's role is to pay attention to the ${jetty.home}/contexts/ directory for any jetty-xml formatted deployable contexts. This deployment mechanism gives you the maximum control over the deployment, the 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 of. 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.

What you'll want to do is to is:

  1. Make sure your ContextProvider based deployments are enabled in the start.ini (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 as that will cause the WebAppProvider to also deploy the same webapp and confusing your deployment.

Finally, you can see how this is done in the jetty distribution itself, just open the ${jetty.home}/contexts/test.xml and look around. You'll see that it loads the ${jetty.home}/webapps/test.war via the ContextProvider's use of the ${jetty.home}/contexts/test.xml into the "/" context path.

Another note, 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 tells me that WebAppContext was

  • Started on {/, (the root Context Path)
  • Using the temp/work directory file:/tmp/jetty-0.0.0.0-8080-test.war-_-any-/webapp/
  • Using the web application specified in /home/joakim/code/jetty/distros/jetty-distribution-7.6.0.RC3/webapps/test.war.

Update: clarifying the statement about the empty context path.

like image 76
Joakim Erdfelt Avatar answered Oct 24 '22 01:10

Joakim Erdfelt


See http://wiki.eclipse.org/Jetty/Howto/Deploy_Web_Applications:

If the webapp is called root.war or the directory is called root/ then Jetty deploys it at the / context.

PS: I've never used Jetty, and it took me 3 seconds to find with Google.

like image 1
JB Nizet Avatar answered Oct 24 '22 01:10

JB Nizet