Flylib.com

Books Software

 
 
 

Setting the Context Root of a Web Application


Setting the Context Root of a Web Application

The context root of a web application determines which URLs Tomcat will delegate to your web application. If your application's context root is myapp , then any request for /myapp or /myapp/* will be handled by your application unless a more specific context root exists. If a second web application were assigned the context root myapp/help , a request for /myapp/help/help.jsp would be handled by the second web application, not the first.

This relationship also holds when the context root is set to / , 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.

The context root for an application is determined by how the application is deployed. 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 a web module. In the following example, the context root of the web-client . war application is set to bank :

<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>bank-ejb.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>web-client.war</web-uri>

<context-root>bank</context-root>

</web>
    </module>

</application>

For web applications that are deployed outside an EAR file, the context root can be specified in two ways. First, the context root can be specified inside the WEB-INF/jboss-web.xml file. The following example shows what the jboss-web.xml file would look like for the same web-client.war file shown previously if it weren't bundled inside an EAR file:

<jboss-web>

<context-root>bank</context-root>

</jboss-web>

Finally, if no context root specification exists, the context root will be the base name of the WAR file. For web-client.war , the context root would default to web-client . The only special case to this naming is the special name ROOT . To deploy an application under the root context, you simply name it ROOT.war . JBoss already contains a ROOT.war web application in the jbossweb-tomcat50.sar directory. So you need to remove or rename that one to create your own root application.

Naming your WAR file after the context root it is intended to handle is a very good practice. Not only does it reduce the number of configuration settings to manage, but it improves the maintainability of the application by making clear the intended function of the web application.


Setting Up Virtual Hosts

Virtual hosts allow you to group web applications according to the various DNS names by which the machine running JBoss is known. For example, consider the server.xml configuration file given in Listing 9.1. This configuration defines a default host named vhost1.mydot.com and a second host named vhost2.mydot.com , which also has the alias www.mydot.com associated with it.

Listing 9.1. An Example of a Virtual Host Configuration
<Server>
   <Service name="jboss.web"
      className="org.jboss.web.tomcat.tc5.StandardService">

      <!-- A HTTP/1.1 Connector on port 8080 -->
      <Connector port="8080" address="${jboss.bind.address}"
                 maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                 enableLookups="false" redirectPort="8443" acceptCount="100"
                 connectionTimeout="20000" disableUploadTimeout="true"/>

      <Engine name="jboss.web" defaultHost="vhost1">
         <Realm className="org.jboss.web.tomcat.security.JBossSecurityMgrRealm"
                certificatePrincipal=
"org.jboss.security.auth.certs.SubjectDNMapping"
            />
         <Logger className="org.jboss.web.tomcat.Log4jLogger"
                 verbosityLevel="WARNING"
                 category="org.jboss.web.localhost.Engine"/>

            <Host name="vhost1" autoDeploy="false"
                  deployOnStartup="false" deployXML="false">
                <Alias>vhost1.mydot.com</Alias>
                <Valve className="org.apache.catalina.valves.AccessLogValve"

                       prefix="vhost1" suffix=".log" pattern="common"
                       directory="${jboss.server.home.dir}/log"/>

                <DefaultContext cookies="true" crossContext="true" override=
"true"/>
            </Host>
            <Host name="vhost2" autoDeploy="false"
                  deployOnStartup="false" deployXML="false">
                <Alias>vhost2.mydot.com</Alias>
                <Alias>www.mydot.com</Alias>

                <Valve className="org.apache.catalina.valves.AccessLogValve"
                       prefix="vhost2" suffix=".log" pattern="common"
                       directory="${jboss.server.home.dir}/log"/>

                <DefaultContext cookies="true" crossContext="true" override=
"true"/>
            </Host>
      </Engine>
   </Service>
</Server>

When a WAR file is deployed, it is by default associated with the virtual host whose name matches the defaultHost attribute of the containing Engine . To deploy a WAR to a specific virtual host, you need to specify an appropriate virtual-host definition in your jboss-web.xml descriptor. The following jboss-web.xml descriptor demonstrates how to deploy a WAR to the virtual host www.mydot.com . Note that you can use either the virtual hostname in the config file or the actual hostname:

<jboss-web>
    <context-root>/</context-root>
    <virtual-host>www.mydot.com</virtual-host>
</jboss-web>