Tuesday, September 27, 2011

Tomcat Tips

Redeploying web applications in Apache Tomcat using touch command

In this post I am going to tell you about a way to redeploy web applications in Apache Tomcat. This is a best practice in using Tomcat in deployment environment.

When testing the deployed web applications in Apache Tomcat, often we need to redeploy them. During the first couple of days I'm using Tomcat, when I want to redeploy a web application, what I always did was, shutting down the Tomcat server and then again starting it. But think for a moment, can we do this in deployment environment ? We can't. Because, when we shut down the Tomcat server just to redeploy a single web application , all the other applications inside that server go down. This is very risky in sometimes.

So, how can we redeploy a single web application inside Tomcat without affecting others ? There is a very simple way, and it's just a command only. It is the "touch command". Here is how to use it.

$ touch <path-to-web-app>/WEB-INF/web.xml

It's as simple as that. :)

What is web.xml file ?  

It is called the web application's deployment descriptor. It contains the configuration details and other useful details of the web application. 

The url mappings are contained in the web.xml file. When the web server receives a request for the application, it uses the deployment descriptor to map the URL of the request to the code that ought to handle the request.

You can read more on web.xml file from here.



Friday, September 23, 2011

Apache Tomcat - Web Application Deployment

Have you ever tried deploying web applications in Tomcat, keeping the war file somewhere other than 'webapps' directory. For example, inside your preferred directory.

Once I had to find a method to do so. So I found out what actually happens when we put a .war file into 'webapps' directory in Tomcat. (This is the conventional way we use to deploy web applications in Tomcat.)

Here is the directory structure of Tomcat




As we all know when we put a .war file in to the 'webapps' directory in Tomcat, while Tomcat is running, it is deployed automatically. During the deployment process, Tomcat unpacks the .war file and creates a directory with the name of the application and keeps it inside the 'webapps' directory. Further, it puts a context XML file inside $CATALINA_HOME/conf/Catalina/localhost/ directory.

These things happen automatically, according to a preconfigured method. But we can deploy web applications, by manually creating the above mentioned context XML file.

Here are the simple steps we can follow in order to deploy web applications in Tomcat server without putting the .war file inside 'webapps' directory.

To do so, we need to create the context XML manually and put it in to the $CATALINA_HOME/conf/Catalina/localhost/ directory.

  1. Keep the application-name.war file inside your preferred directory.
  2. Create the context XML file with the name application-name.xml 
  3. This context XML file should contain the "Context element" as below
  4. Put the context XML file inside $CATALINA_HOME/conf/Catalina/localhost/ directory.

Once you put the context XML file inside the above mentioned directory, Tomcat search for the .war file (or the unpacked web application folder) in the location where you given as the "docBase" attribute value in the above mentioned "Context element". Then it will unpack the .war file in to the "webapps" directory.
So now the web application is deployed. :) 

Note : Remember, if you put the context XML file inside Tomcat , before placing the .war file in the location you mentioned, Tomcat will throw an exception saying it cannot find the war file in the location you've mentioned.




Thursday, September 8, 2011

Apache POI - The Java API to create Microsoft Office Documents

Apache POI is the Java API created to access Microsoft format documents using Java.

For example you can write data to an excel sheet using POI.

To write to an excel sheet first you need to create HSSFWorkbook object. This represents a Work Book in excel. Then you can create number of work sheets in this work book.

One a sheet is created you can get the rows in it one by one. These rows consist of cells. The data you need to write in the excel sheet should go inside these cells by providing them as the cell value. Further, these cells can be formatted to have different types of data inside them, such as numeric type, formula type, etc.