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

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!

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:
Make sure your ContextProvider based deployments are enabled in the start.ini (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 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.

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.

Related

Tomcat set application context using configuration (not war name)

My question is this - Say I have a war file called my-app-123.war. I want to deploy it on a Tomcat server (9.0.x), let it auto unpack.
The application would then be accessible by http://localhost:8080/my-app-123
Is there a way, without renaming the war file, to make the application accessible from http://localhost:8080/my-app?
I will preface this by saying I realize the easiest solution is to just name the war file. I'm curious if there is a way to do this in Tomcat configurations.
I did do this already (inside the host section of my server.xml file):
<Context path="/my-app" docBase="my-app-123"></Context>
But I read this online (https://tomcat.apache.org/tomcat-7.0-doc/config/context.html) in the path variable description:
Even when statically defining a Context in server.xml, this attribute must not be set unless either the docBase is not located under the Host's appBase or both deployOnStartup and autoDeploy are false. If this rule is not followed, double deployment is likely to result.
And I can access the app now at http://localhost:8080/my-app and http://localhost:8080/my-app-123, which isn't a real solution.
The following works for Tomcat deployments I have used - there are no double-deployment issues.
In the example I will use here, I have a simple "hello world" webapp in TomcatDemo-1.0-SNAPSHOT.war. It is deployed on Tomcat 9.0 in the standard location (webapps directory).
I want the application's context path to be /my-foo-app.
I use the following context entry in server.xml:
<Context path="/my-foo-app" docBase="TomcatDemo-1.0-SNAPSHOT.war"></Context>
I also use the following in server.xml:
<Host name="localhost"
appBase="webapps"
deployOnStartup="true"
deployIgnore="TomcatDemo-1.0-SNAPSHOT.war"
unpackWARs="true"
autoDeploy="false">
The important item here is deployIgnore. It is described here:
https://tomcat.apache.org/tomcat-9.0-doc/config/host.html#Automatic_Application_Deployment
When using automatic deployment, the docBase defined by an XML Context file should be outside of the appBase directory. If this is not the case, difficulties may be experienced deploying the web application or the application may be deployed twice. The deployIgnore attribute can be used to avoid this situation.
Alternatively, if you don't mind about automatic start-up deployments, you can set deployOnStartup="false" - in which case you don't need deployIgnore.
In these scenarios, the web app is available only here:
http://localhost:8080/my-foo-app/ <-- OK
Otherwise, as you note, with double-deployment the web app would also be available here (and you would see two exploded directories in webapps):
http://localhost:8080/TomcatDemo-1.0-SNAPSHOT/ <-- BAD!
Hope that helps.
Finally, it can get a little complicated, with all the various auto-deployment options. This page provides a set of summary tables and explanations:
https://tomcat.apache.org/tomcat-9.0-doc/config/automatic-deployment.html

Java configure context root of web application

I have a Java web application using Wicket 6, Spring 3.2 and WildFly 8.2.0. Right now i'm setting the context root of my web application in the jboss-web.xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
<context-root>/myCustomContextRoot</context-root>
</jboss-web>
The jboss-web.xml file is compiled into the war. Now some clients want to change this context root to an empty context root. So i hace to recompile a version of my app per different context root. Is there a way to set the context root of my application from outside .war, programatically from a .properties file, or any other way for example in standalone.xml of WildFly 8.2.0?
Set the runtime name when deploying your web application. Suppose your WAR is called myapp-1.0.0-SNAPSHOT.war. Using a runtime name of foo.war, the context root will be /foo.
Using a runtime name of ROOT.war, the context root will be /.
The runtime name can be set when deploying via the Web Console or via the CLI.
Thanks for your Answer Harald Wellmann. It answers the question and pointed me into the right direction!
Some things I had to find out on my own and which may help others:
the exact syntax in jboss-cli to specify a runtime-name is:
deploy path_to_war_file --runtime-name=wantedName.war
This leads to a context-root of /wantedName/ for the webapp.
The runtime-name does not have any effect on the context-root, if the war file contains a jboss-web.xml in WEB-INF which in turn contains a context-root tag.
That is, if you want to control the context-root of your web-app in WildFly at deployment time, you must not specify any context-root in jboss-web.xml.
It is ok to have a jboss-web.xml without a context-root tag if you want to take advantage of the runtime-name to control the context-root.
I tested this on WildFly 9.0.1 and 9.0.2:
Hope this helps!

How to proxy different WARs in Tomcat 7?

I'm developing a web application, which consists of two independent parts - the authentication and the real application part. Both parts are WARs which are deployed at (currently) one Tomcat 7 instance.
So, I have the following two WARs in my webapps folder:
webapps
|
+- BloggofantAuthentication
|
+- Bloggofant
until now they are available at http://127.0.0.1:8080/BloggofanAuthentication and http://127.0.0.1:8080/Bloggofant. Is it possible proxy the WARs at Tomcat directly (so that I don't have to use Apache httpd and its mod_proxy module)? So that in the end, the WARs at the server are reachable as follows:
http://127.0.0.1:8080/BloggofantAuthentication -->
http://127.0.0.1/bloggo/
http://127.0.0.1:8080/Bloggofant -->
http://127.0.0.1/bloggo/fant/
Any suggestions on this topic are highly appreciated ;)
EDIT
Here are the context.xml files of the two unpacked webapp WAR folders:
webapps/BloggofantAuthentication/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="">
<!-- Comment this to enable session persistence across Tomcat restarts -->
<Manager pathname=""/>
</Context>
webapps/Bloggofant/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/bloggofant">
<!-- Comment this to enable session persistence across Tomcat restarts -->
<Manager pathname=""/>
</Context>
If I now want to access my apps via http://127.0.0.1:8080 or http://127.0.0.1:8080/bloggofant I get a 404 - Page Not Found error ...
You can configure the path at which Tomcat serves a web application using a context.xml file. You can put this in the WAR's META-INF directory, with the content:
<Context path="/bloggo/fant" />
And it will serve it there instead of at the default /Bloggofant path.
Note the warning about automatic deployment in the documentation:
When autoDeploy or deployOnStartup operations are performed by a Host, the name and context path of the web application are derived from the name(s) of the file(s) that define(s) the web application. Consequently, the context path may not be defined in a META-INF/context.xml embedded in the application
Elsewhere, the documentation tells us that these both default to true. Thus, you will need to set them to false for these settings to be respected.

Relative path in Context.xml

Is there a way to set a relative path to the docBase attribute in the context.xml of a web application, so it is outside of the appBase directory of the tomcat server instance?
I would like to be able to share the context configuration between computers and have the app living in a directory, not a war file. That way i can compile the classes directly into that directory (in my project development directory) and have tomcat use these classes without any copying/packaging needed.
I am using the tomcat 8.0.0-RC5.
My directory Layout is:
/home/david/projects/frontend/web-content <-- the static html files
/home/david/projects/frontend/web-content/WEB-INF <-- the WEB-INF with the web.xml
/home/david/projects/tomcat <-- tomcat base directory
/home/david/projects/tomcat/Catalina/localhost <-- holds the frontend.xml context configuration
I have tried
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="../../frontend/web-content">
</Context>
but that did not work. The whole path before /web-content seems to be ignored. The log says:
The main resource set specified [/home/david/projects/tomcat/webapps/web-content] is not valid
The Tomcat 8 documentation for the context container says:
You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.
Does relative here mean a strict subdirectory of appBase (no .. allowed)?
Setting an absolute path works without problems. The configuration
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="/home/david/projects/frontend/web-content">
</Context>
works, but it is specific to my computer. So I cannot share the context configuration without modification anymore.
I could create a symbolic link inside the appBase directory of the tomcat server and let it point to the web-content folder of my application. This would work, but I would have different configurations (symbolic links) on linux and windows machines.
Just taking only the last name of relative path names and ignoring the first parts
is most likly a bug in tomcat. Relative path names should work or must throw errors.
But ignoring parts is bad.
A workaround could be using an absolute path name like this:
<Context docBase="${catalina.home}/../www.war"
I just reading the changelog of Tomcat 8.0.15.
The bug should be fixed(!):
"Correctly handle relative values for the docBase attribute of a Context."

customize web application root context in apache

I have web application abc.war and I want to deploy it on Apache Tomcat.
The problem is that, by default, the path to this application is http://<server-name>/abc
but I want to access it as http://<server-name>/xyz.
I put into WAR's META-INF folder the file context.xml that is :
<Context path="/xyz" docBase="abc" override="true" />
The application WAR abc.war is located under %CATALINA_HOME%\webapps and it is extracted to %CATALINA_HOME%\webapps\abc folder.
Also, while deployment, the file context.xml from abc/META-INF is copied to %CATALINA_HOME%\conf\Catalina\localhost as abc.xml
It seems that this should work, but I still can't access my application through http://<server-name>/xyz, but only through http://<server-name>/abc
In addition, I still see in apache log the following line while deployment of abc.war :
context path = /abc
Could anybody, please, help while this doesn't work, or tell if there is any way of deploying of web application on apache such that application could be accessed by customized path (that does not relate to war-file name) ?
Thanks in advance.
Take a look at the docs:
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.
The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

Categories

Resources