Tomcat

Tomcat is a simple, fast, hard to break program that works as a web and application server. Web server means that it can response to requests over the web for HTML pages or any other files. Application Server means that it can run Java code within itself of arbitrary complexity to generate and deliver programmed HTML pages. That's Tomcat.

This tutorial is more aimed at someone using Tomcat for the first time than someone tweaking JBoss or Websphere for the third time.

The web page for Tomcat on the Internet is http://tomcat.apache.org

Understanding Servers, Protocols, and Tomcat

If you are new to web programming, this section will interest you.

Servers

What is a server? Imagine that you are playing cricket or baseball with a friend - and that everytime your friend throws the ball at you, you bat it back into his gloves right where he expects it.

This kind of cooperative behavior is a good model for understanding client-server programs. The client throws at the server a ball with a request for written on it - and the server bats it back to the client with a response. The analogy gets weak if you consider that a server response is very different from a request - "not the same ball" - but if you consider the information written on the ball - then we are in good shape - if the ball is hit in a way that the message is written on to it.

Server and client programs just respond to requests from the client to the server - they are just two processes which may or may not run on the same machine. Server and client are not intended to describe machines but processes.

Protocols

As a baseball or cricket ball moves through physical space, so a client's request to a server has to move through a physical space of wired and radio transmission. The example that is commonly given is that of two Army Generals working out a logistical problem of moving a truck from one city to another.

So the General tells the Colonel what to do and the Colonel tells his junior rank a Captain what to do - and the Captain in turn gives the truck driver detailed instructions for each city.

In our case, HTTP is the client-server protocol of the web. It is the General - the topmost commander of the network stack. HTTP tells TCP under it what messages to deliver and TCP makes sure they are delivered reliably on a potentially unreliable network.

TCP (the Colonel)  in turn tells IP - the Captain - what address on the network to send the message to and IP complies thus enabling the still lower level protocols to deal with the physical transmission (logical and physical now).

Web Server

A web server then is a program that responds to client requests transmitted to it via the HTTP protocol. It responds to client requests for files on a given web address by picking up a file on its own storage and transmitting it to the client.  The file is typically HTML as this one is - a script that the browser requesting it can interpret for display to the user. Tomcat contains within it a web server.

Application Server

An application such as eBay, Google or Yahoo - which deals with large volumes of structured information which are only best represented at the server computer using relational or other databases (relational is the most common) - there needs to be a "container" at the server end which can run actual programs within itself to organize the server side data store in conjunction with the user transmitted by the user.

For example if a Yahoo mail user enters a username and password the server should fetch the user's message Inbox from the database when the Inbox hyperlink is pressed by the user - not some other user's information and not the Sent mails.

This functionality is called an application server. Tomcat also contains within it an application server.

Tomcat is web and application server. Apart from the database this is the biggest chunk of what we need - and once you understand this - then all you're left with is understanding the subtleties of actually running your programs using Tomcat as a server.

What I have said is also true of J2EE servers such as JBoss or BEA Weblogic.

Tomcat is the simplest of the lot so you should try this first as a production server - it is also brilliantly engineered.

Downloading Tomcat

The official web page for Tomcat is http://tomcat.apache.org. The downloads are prominently shown on the left hand side of the page.

Before installing Tomcat, you will need to have a Java Development Kit installed on your computer. Among JDK versions, I find 1.4.2 the simplest of the current lot. Among Tomcat versions, I find 5.0.28 the simplest of the current lot. Tomcat 5.5 requires the JDK or Java Runtime Environment 1.5.

My general algorithm for choosing software is - the simplest that does the job.

I will show you how to install this version of Tomcat on Windows. It remains true that while most deployment servers are Linux, most development machines are Windows - and you will need to develop before you deploy.

Installing Tomcat

The steps for installing Tomcat 5.0.28 on Windows are shown on this page as screenshots.

Install Tomcat by double-clicking on the downloaded installer. You will need to define and create an admin password during the installation. Note it somewhere you will remember. Forgetting the admin password on these systems is associated with untold complexities.

You should check on your Java configuration to make sure the environment variables for Java are set to their correct values - PATH, JAVA_HOME, and CLASSPATH. Set the variable TOMCAT_HOME which is the top level path for Tomcat and which on my development machine is C:\Program Files\Apache Software Foundation\Tomcat 5.0. The last entry for PATH and CLASSPATH for Java should be ";." for your convenience (semi-colon followed by full-stop). This will make it possible for you to compile and run Java programs in arbitrary directories of your choice.

Starting Tomcat

The installer asks you whether to run Tomcat directly as a service. If you say Yes to that, you have Tomcat running as soon as you have installed it (if this does not happen, go to the home directory for Tomcat and type

bin\shutdown
and then
bin\startup

After that, it is just a question of putting the right files in the right Tomcat directories and then running your server side programs by starting Tomcat with the right files in the right directories. You may need to restart Tomcat at times. This is easy to do as explained just above - the bin\startup and bin\shutdown commands.

That's all there is to it.

If you have downloaded and installed Tomcat correctly, you should be able to type http://127.0.0.1:8080 in your browser address bar and see a window into the entire Tomcat documentation now visible to you from your local machine with the Tomcat server running on it and serving requests to your window. Similarly, across your local network, people should be able to type

http://your_ip_numerical_address_or_domainname:8080

to see any pages you want to serve to them via your newly installed Tomcat server.

If you want to click on the links Tomcat Administration or Tomcat Manager to the top-left when you look at the documentation on your machine via http://127.0.0.1:8080 , you will want to use the username and  admin password you entered recently for Tomcat while installing.

Running and Using Tomcat

Now you can directly serve the simplest parts of J2EE - flat HTML files and JSP pages using Tomcat. There's an extraordinary amount you can do with JSP and Tomcat - entire running complex web sites. This is what we will dip into and explore a bit as part of our excursion into the simplest parts of J2EE (I never said it would be less powerful) - Tomcat with JSP has all the power of PHP, with the simplicity of Java right there if you're willing to restrict yourself to a subset of Java for you internal organization.

To see some action right away, create a directory say mywork in your webapps directory of Tomcat - put in it a file called index.html in which you put the HTML of your choice - and then click on the Tomcat Manager link to your top-left of the Tomcat screen on your web browser. Log in and under the Deploy text, put the name of your directory for the entry " WAR or Directory URL: ". You should now be able to call this file from your web browser http://localhost:127.0.0.1/mywork and connect to all legitimate URLs it links to.

That's all it takes to get started with Tomcat.
Click here to see how much you can do with just JSP and Tomcat. In my view, PHP is like J2EE without the Servlets and just the JSP. If you're a PHP professional you know more than I do - but check out how much power is yours just now with this technology foundation of JSP. You can run JSP files as easily as you can call HTML here - and that is the least you should make sure you do having come this far. The page is the same as the one you get by clicking on the JSP link on the main page of this site.