Sunday, November 15, 2009

Deploying Web-Apps on Tomcat

Today, we are going to see various methods that can be used to deploy a web-application using Tomcat. These techniques are pretty simple and are a must for every web developer. However, most of us are only aware of a few of these.

Lets say that on your machine, Tomcat is installed at E:/Tomcat/apache-tomcat-6.0.10/. Hereafter, we will refer this location as {TOMCAT_HOME}

Also suppose that your web application is named as TestWebApp and is located at E:/MyWorkSpace/Java/TestWebApp. Hereafter, we will refer to this location as {WEBAPP_HOME}, and the output folder is named as web.

The Three Methods to deploy any web application on Tomcat are discussed below:

First Method – Copy & Paste

The simplest and the most straight-forward method to deploy any web application on Tomcat is by copying the output folder or the WAR (Web Application Archive) file of your application to webapps folder of your Tomcat installation as shown below:

a) Copy the output folder

Simply copy the output folder of your web application to {TOMCAT_HOME}/webapps/ folder. Note that you must take care that your output folder must have a folder named WEB-INF, which further contains web.xml file. This file is also known as deployment descriptor as it governs how a web application should be deployed. Now restart tomcat to see the effect.

FYI, the Tomcat Home, or the Tomcat Home page you see when you type http://localhost:8080/ is also deployed this way and the output folder is named as ROOT.

b) Copy the WAR file

First, create a WAR file by going to {WEBAPP_HOME}/web and then type:

> jar –cvf * TestWebApp.war

This will create a WAR file for you, which you have to copy to {TOMCAT_HOME}/webapps/ folder. Now, restart tomcat to see the deployed application.

Second Method – Server.xml

Another way to deploy your Web application is to add a context entry in Tomcat’s server.xml file, which can be found at {TOMCAT_HOME}/conf/server.xml

Edit this file by adding the following entry:

<Context path=”/myApp” docBase=”E:/MyWorkSpace/Java/TestWebApp/web” debug=”true” reloadable=”false” />

where myApp is now the Context for you deployed application. To see the deployed application, simply restart tomcat and access the deployed application at http://localhost:8080/myApp/

To deploy more applications, simply add one Context entry per application. Note that the path attribute should be unique for all deployed applications at one time.

Third Method – Context in an external XML file

Create a XML file containing the Context Entry for your application

<Context path=”/myApp” docBase=”E:/MyWorkSpace/Java/TestWebApp/web” debug=”true” reloadable=”false” />

You can give any name to this XML file, but a common practice is to name it in such a way that helps you identify it with the application it deploys. We name is testWebApp.xml.

All that is left now, is to copy this testWebApp.xml file to {TOMCAT_HOME}/conf/Catalina/localhost/ folder.

Restart tomcat and voila, you are done. This way, we have one XML file per application, containing the context and location for that application.

Comparing Various Methods

The first method is pretty simple and straight-forward but it restricts you to copy the output folder to Tomcat Installation, something that you would not want to do every-time or you may not have enough access rights in some cases.

On the other hand, the second method is flexible in the sense that location of the output folder of your web application is independent of your Tomcat installation. The downside is that deploying and un-deploying applications again and again can be a head-ache as you have to edit the same server.xml file again and again.

The third method rules !! It not only is flexible as its second counter-part but also it deploys applications independent of each other in the sense that each application has its own context file (xml file). In case you want to deploy some applications, while restrict some other, you simply have to move/delete the context files (xml) for the applications you do not want to deploy.

Any Comments, Suggestions or Corrections are welcome