Version 9

    By default, the context root of the web application (also called the context path) is set to the root name (the name without the .war extension) of the Web Application Archive (WAR). You can override the context root using the standard application descriptor (i.e., application.xml) for EAR deployments or the JBoss AS web descriptor (i.e., jboss-web.xml) for standalone WARs. Let's talk about standalone WARs first.

     

    Standalone WAR deployment

     

    You override the context path of the web application (the context root) in a standalone WAR in a jboss-web.xml, located in the WEB-INF directory of the WAR. Here, we are setting the context root to /, which which is known as the root context. When an application is assigned to the root context, it will respond to all requests not handled by a more specific context root.

     

    /WEB-INF/jboss-web.xml (in WAR)

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web>
        <context-root>/</context-root>
    </jboss-web>
    

     

    There are many other settings this file can configure. Although it's not required, you can add an XSD (or in earlier versions a DTD) to the XML root. Let's add the XSD for JBoss AS 5.1, found in the JBoss AS schema catalog, and change the context root to /myapp.

     

    /WEB-INF/jboss-web.xml (in WAR)

    <?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> 
    

     

    (Note: If you have a problem deploying, simply do without the schema declarations).

     

    While the manner in which the context path is set in a standalone WAR is proprietary, Java EE does provide a standard way of assigning a context path to a WAR when deployed in an EAR.

     

    EAR deployment

     

    When a web application is deployed inside an EAR file, the context root is specified in the application.xml file of the EAR, using a context-root element inside of a web module. In the following example, the context root of the bank-webapp.war application is set to /mymoney.

     

    /META-INF/application.xml (in EAR)

    <application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
          http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/application_5.xsd">
       <module>
          <ejb>bank-ejb.jar</ejb>
       </module>
       <module>
          <web>
             <web-uri>bank-webapp.war</web-uri>
             <context-root>mymoney</context-root>
          </web>
       </module>
    </application>