Java configure context root of web application - java

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!

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

404 error when I redeploy a war on Wildfly

I tried to deploy a war file on Wildfly (commandline) by changing the name of the old file (say app.war to appOld.war) and copying a new file with the name app.war to the deployment folder.
On my other terminal, I can see the auto deploy scanner running and deploying the new file but when I try to access the app via URL, I get a 404.
No error shows up in the logs so I don't realize what is happening or what to do.
Thanks.
I think there is problem with your context root.
Because if you don’t set context root wildfly takes filename as your context root.
When you deploy file you just renamed try access <hostname>:<port>/appOld instead of <hostname>:<port>/app
Context root can be manually set set in /WEB-INF/jboss-web.xml
Here is the example of jboss-web.xml whit context root:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/my-web-app</context-root>
</jboss-web>
So when you set it you should be able to access yout app at: <hostname>:<port>/my-web-app
Hope it helps.

Load external library in java web application

My scenario is the following:
I have a WebApp.war that is deployed to a servlet container. This WebApp.war contains in WEB-INF/lib the following libraries:
lib_a.jar
lib_b.jar
I have one other library, say lib_vendor.jar, that I cannot deploy within WebApp/WEB-INF/lib because of licensing issues so I let our customers to copy this library in tomcat/lib after application installation. But since lib_vendor.jar requires lib_a.jar and lib_b.jar that are loaded in the web application class loader, I cannot use lib_vendor.jar.
How can I load an external library (not in WEB-INF/lib) in the same classloader of a web application?
Since you are using Tomcat, you could leverage the VirtualWebappLoader.
Add a META-INF/context.xml whith
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/somepath/myapp">
<Loader className="org.apache.catalina.loader.VirtualWebappLoader"
virtualClasspath="/somedir/*.jar"/>
</Context>
Remember also that the virtualClasspath attribute must be a absolute path, as correctly stated in the comment below.

URL to access RESTful WebApp deployed to Tomcat 7.0

I have been learning RESTful webservices following this tutorial http://www.vogella.de/articles/REST/article.html. As I understand, the url to access the rest service is
http://your_domain:port/display-name/url-pattern/path_from_rest_class
and that the display-name is configured in web.xml. However the actual url is
http://your_domain:port/**war_fileneme**/url-pattern/path_from_rest_class
Is this correct?
the url would look awkward if war filename also contained version info. So is it possible to override this?
I am using Tomcat 7.0, Jersey and Eclipse IDE.
Thanks.
this is the context path.
since you can have multiple contexts in tomcat, each one has to have its own context path, and by default tomcat uses the war filename prefix, but if you deploy in tomcat's ROOT webapp directory you can access your webapp at
http://your_domain:port/display-name/url-pattern/path_from_rest_class
otherwise it's always:
http://your_domain:port/context/display-name/url-pattern/path_from_rest_class
but you can alter this value by choosing an apppropriate context path in web.xml:
something like
<context path="mypath">
...
</context>
should yield:
http://your_domain:port/mypath/display-name/url-pattern/path_from_rest_class
check here for some info:
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Attributes
hope that helped...

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