How to set the Web Context Path in WildFly

This short article will teach you how to change the Web Context Path for applications running on WildFly or JBoss EAP application server

Context Path defaults

Firstly, by default, a Web application inherits the Web Context Path from its name. Therefore, a Web application whose name is helloworld.war, will be available under the http://localhost:8080/helloworld Context Path

If you are using Maven to build your project, the final application name is determined by the artifactid. However, you can override it through the finalName element of the build section:

<build>
      <finalName>${project.artifactId}</finalName>
      <!-- -->
</build>

Choosing a different Context Name

The recommended option to set a non-default Context name for a Web application is to add the jboss-web.xml in the WEB-INF folder of your Web application:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://www.jboss.com/xml/ns/javaee
      http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
   <context-root>/myapp</context-root>
</jboss-web>

There, you can choose the Web application context through the context-root element as you can see from the above example.

If you are packaging your Web application in an EAR file, another option is to use the context-root element from the application.xml file:

<application xmlns="http://java.sun.com/xml/ns/j2ee" version="1.4"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com /xml/ns/j2ee 
                             http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
    <display-name>JBossDukesBank</display-name>

    <module>
        <ejb>example.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>webapp.war</web-uri>
            <context-root>myapp</context-root>
        </web>
    </module>

</application>

Changing the Web Context of a deployed Web application

Finally, it’s possible to change the Web context of a Web application by using a CLI hack. Follow these steps:

  1. Create a file jboss-web.xml with the Web Context. In our example, we will place it in the /tmp folder
  2. Connect to the CLI (jboss-cli.sh)

Once connected, apply the new Web Context using the Deployments Overlay feature:

deployment-overlay add --name=myOverlay --content=/WEB-INF/jboss-web.xml=/tmp/jboss-web.xml --deployments=helloworld.war --redeploy-affected

In this example, the affected deployment is helloworld.war. You can apply the change to multiple applications, even using a regular expression such as “*”. See this example:

deployment-overlay add --name=myOverlay --content=/WEB-INF/jboss-web.xml=/tmp/jboss-web.xml --deployments=helloworld.war,*-admin.war --redeploy-affected

To learn more about deployments overlay, check this article: How to add a web fragment to all applications deployed on WildFly

Finally, if you want to set as Web Context the Root Web Context (“/”) check this tutorial: How to deploy a Web application on the Root Context on WildFly?

Found the article helpful? if so please follow us on Socials