Sunday, May 2, 2010

How do I make my web application be the Tomcat default application ?

You have created and tested a first web application (traditionally called "besthowtodo"), users can access it via the URL "http://besthowtodo.company.com/besthowtodo". You are very proud and satisfied. But now, how do you change the setup, so that "besthowtodobesthowtodo" gets called when the user enters the URL "http://besthowtodo.company.com" ?
The pages and code of your "besthowtodo" application currently reside in (CATALINA_BASE)/webapps/besthowtodo/. In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, there is a directory called ROOT (the capitals are important, even under Windows). That is the residence of the current Tomcat default application, the one that is called right now when a user calls up "http://besthowtodo.company.com[:portNumber]". The trick is to put your application in it's place.
First stop Tomcat.
Then before you replace the current default application, it may be a good idea to make a copy of it somewhere else.
Then delete everything under the ROOT directory, and move everything that was previously under the (CATALINA_BASE)/webapps/besthowtodo/ directory, toward this (CATALINA_BASE)/webapps/ROOT directory. In other words, what was previously .../besthowtodo/WEB-INF should now be .../ROOT/WEB-INF (and not .../ROOT/besthowtodo/WEB-INF).
Just by doing this, you have already made you webapp into the Tomcat default webapp.
One step is left : you also need to have, within your application, a default servlet. This, you do by means of an appropriate url-mapping in the WEB-INF/web.xml configuration file of your application. Make sure you have something like this in that file :
<servlet>
<servlet-name>Best HowToDO Servlet</servlet-name>
<servlet-class>com.besthowtodo.tutorials.Servlet.Number1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Best HowToDO Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Restart Tomcat and you're done.
Call up "http://besthowtodo.company.com/" and enjoy.
Addendum 1 : If you are deploying your application as a war file..
The above instructions relate to the situation where you are "manually" deploying your application as a directory-and-files structure under the /webapps directory. If instead you are using the "war" method to deploy your application, the principle is about the same :
- delete the ROOT directory
- name your war file "ROOT.war" (capitals mandatory)
- drop the ROOT.war file directly in the /webapps directory.
Tomcat will automatically deploy it.

No comments:

Post a Comment