How do I map a web application to a url in jboss?
If you have multiple web apps deployed in a single jboss instance, you’ll probably want to figure out an effective way to tell them apart when you try to access them from a browser. On startup jboss can be configured to bind to a single url which will act as the default host for all the deployed applications. You can then set up a separate context for each web app you are running. If they’re totally separate applications though, it might not make sense to use a single url and break them out by context. In Jboss you can set up virtual hosts to solve this dilemma. Here’s how to set this up:
WEB-INF/jboss-web.xml
In your web application you’ll want to add an xml file named “jboss-web.xml” to your WEB-INF folder. This is the file that’s going to map both the web application’s context and host in jboss.
<jboss-web>
<context-root>/</context-root>
<virtual-host>www.first-application.com</virtual-host>
</jboss-web>
This configuration sets the application’s context to “/” (essentially the root of the default domain), and it also maps the virtual host configuration to “www.first-application.com”. Note that it wont matter if you deploy this configuration from within an ear (embedded war file) or as a standalone war file, as only wars are meant to respond to web requests. Let’s also add the second configuration to the other war file’s WEB-INF/jboss-web.xml:
<jboss-web>
<context-root>/</context-root>
<virtual-host>www.second-application.com</virtual-host>
</jboss-web>
Next, we’ll need to add the virtual host configurations to jboss’ server.xml.
jbossweb.sar/server.xml
Now we need to edit jboss’ server.xml file, adding the virtual host mappings:
<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="www.first-application.com">
<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="www.first-application.com" autoDeploy="false"
deployOnStartup="false" deployXML="false">
<Alias>dev.first-application.com</Alias>
<Alias>qa.first-application.com</Alias>
<Alias>test.first-application.com</Alias>
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log."
suffix=".log"
pattern="common"
directory="${jboss.server.log.dir}"
resolveHosts="false" >
</Host>
<Host name="www.second-application.com" autoDeploy="false"
deployOnStartup="false" deployXML="false">
<Alias>dev.second-application.com</Alias>
<Alias>qa.second-application.com</Alias>
<Alias>test.second-application.com</Alias>
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log."
suffix=".log"
pattern="common"
directory="${jboss.server.log.dir}"
resolveHosts="false" >
</Host>
</Engine>
</Service>
</Server>
Note the alias map other optional domains that jboss would listen for as aliases of the keyed domains “www.first-application.com” and “www.second-application.com”. All this means is jboss will redirect requests for processing to the respective web application’s whose virtual-host config maps to this host configs.
In order for all of this to work however, we need to make sure dns is set up to handle these domain requests. On a local development machine, you’ll want to edit your hosts file (on windows: c:/WINDOWS/System32/drivers/etc/hosts, on linux: /etc/hosts) and add these entries:
120.0.0.1 www.first-application.com 120.0.0.1 www.second-application.com
This way when you type in one of the domains into your browser, it’ll forward the request to jboss’ bound IP. likewise, if you have jboss bound to a specific domain name/IP on boot, you would have to map that domain/ip in your hosts file just like in the example above.
Now you should be able to fire up jboss, type in either domain into the browser, and have jboss redirect those requests to the corresponding deployed war file.
Related posts: