Downloading and Installing | Checking it out | What Does Tomcat Do? | Sysdeo | Book & Links | Back to Freestuff Home

Tomcat

Tomcat gets the job done. What is Tomcat?  In short, it's the software for a web server (stand alone or with another web server) so you can use Java to create Servlets and JSP and have this content "served" or used by users on the World Wide Web.  So, if you ran a web server you could write a program to take input via the World Wide Web, change it, use it, do whatever somehow and then send your results back to a person's browser.  That's terribly simplified, but that's the gist.  We'll get slightly more complicated as we go, but that's the basics.  It's just a program that takes care of a lot of behind the scenes stuff so you can concentrate on writing the logic of your program for the web and not worry about how it's served out to browsers.  Think of it as a black box where you don't have to worry about every little detail of the entire setup, you just write the logic for your program, stick it inside this box (Java people would call it a "Container") and let it worry about things like memory management, networking and security.  All you have to do is learn the basics of how to work with the Tomcat and it's easy to create powerful and robust code for the World Wide Web with Java.

We will first go and get a copy of Tomcat to install and install it.  Then we'll talk about how to turn it on and off and how to use it locally on your PC to create web apps that you can later move to a real serving environment.  Then we will go through a very modest first example, explaining how you use it to create web apps,  and then I'll give you some tips and URL's.  As with everything else on this site, there's tons of documentation out there on the web, and I'll point you towards some.

Downloading and Installing

http://tomcat.apache.org/

When you visit the above URL you'll see the main page for the Tomcat project (Tomcat is part of the Apache Software Foundation.  Long story short, they, Apache, are a non profit corp that supports open source software that a decentralized group of developers maintain and support.  It has different levels and tons of different projects.  I'll leave that to you to discover later...) and can spot the download center to the left.

  Click on the link for Tomcat 5.x...

(While version 6 is out now, we stick with 5.5.9 because we don't need the newer features and 6.X is buggy on Windows XP boxes)

Underneath the bar that says 5.5.12 and a second bar that reads "Binary Distributions" AND under a bullet for Core click on the link that says "Windows Executable" (ignore the pgp and md5 for now)

 

A Download window should appear.  Navigate to your trusty C:/Temp dir and download the software...

 

Now, from the Temp dir, double click the binary you just downloaded...

 

You'll see the usual setup wizard splash page you're used to...  Just follow the instructions...

 

IMPORTANT!

When you get here make sure to click the bottom two boxes.  They are unchecked by default, but you want the Examples and the Webapps so check those boxes!

 

IMPORTANT!

When you get here REMEMBER THE PASSWORD YOU INPUT.  It'll be important later....

 

Then just let it run and run and run...

 

And finally you'll come to the above.  Congrats.  You now have Tomcat loaded on your PC/laptop.  Go ahead and leave the "Run Apache Tomcat" box checked and click "Finish".  You should look at the README also, but we know how that goes... You look there when things mess up and only then.  It's cool.  I hear ya.

 

Checking it out

The easiest way to check it out is to use it.  Remember, Tomcat is a server that serves web applications, but it also serves web pages too.  When you did the install you told it to load the examples and the webapps so you have content sitting there waiting to be looked at.  Let's look at it. 

Now, we loaded Tomcat on your local machine, so we're going to point a web browser at our local machine.  I use Firefox (because it's free, free, free -  That and you can't use the "Pimpzilla" skins with Explorer) so I'm going to point my browser to "localhost".  Remember networking 101 - the local machine has an address, 127.0.0.1, that refers to itself (it's called the loopback.  Maybe you've heard it called that before.  It's a way to refer to the local machine and test network software without having a packet actually go anywhere... I digress..).  I'll bet dollars to donuts that you have a host table on your PC/laptop that tells the networking that if it sees "localhost" to use 127.0.0.1.  You can use either....

OK.  So what happens when I point my browser to http://localhost and what happens?

Stop lookin' at the cheerleader.  As you can see, I went to localhost and nothing happens!  I get an error!  Huh?  That's because I went to the wrong place.  A web server listens to requests that come in over port 80.  That's the port for HTTP, the protocol that the web uses.  I don't have a web server running on my local PC so my browser gave me an error. 

Tomcat uses HTTP too, but it doesn't listen to port 80.  Tomcat doesn't want to mess with an existing web server if one exists so when it installs it sets itself up to listen for requests on a different port than port 80 (You can configure Tomcat to work with web servers, particularly Apache, but that's beyond the scope of what we're doing.  We just want to get Tomcat working!).  So what we have to do is tell our browser what port to send the request to so we can get our request to the port that Tomcat is listening on.  Tomcat listens for requests on port 8080 as a default (This can be configured if you want to change it)

So let's try http://localhost:8080

Now we're talkin'.  This is the main page at the base of a Tomcat installation.  If you're seeing this then you're installed OK.

 

There's two other things we can do real quick to verify that we're all loaded properly.  We can look at the installation in Windows Explorer and see if the Tomcat installation looks like it should.

That is a healthy looking base install for Tomcat.  The main directory (sometimes called CATALINA_HOME) is C:\Program Files\Apache Software Foundation\Tomcat5.5.  Take a look at the different directories.  As you learn how to use Tomcat you will learn what each of them is for and how Tomcat works with them.  If you see webapps and common and conf then you're in good shape.

Another thing you can do is check the "services" to see if it's running.  Windows runs some programs as "services" and not as regular programs.  It gets a little technical, so for now we'll just go take a look and you can Google "Windows Services" and learn the difference for yourself.  You go there by going to "Control Panel", double clicking "Administrative Tools" and then double clicking "Services". 

And there she is.  This is the way to turn Tomcat on and off as well.  If you right click the highlighted bar you will see the option to Start, Stop, and Restart (There are other ways to do it, but this is the preferred method.  You'll see that a default installation on a Windows box has Tomcat as a service that's started on boot by default.  If you want to turn it off you come here, right click and stop the service.)

Tomcat didn't always work like this.  The older versions ran like regular old programs and you had DOS-like batch files called startup.bat and shutdown.bat to start and stop it.  Those days are over.  Now Tomcat is run as a service just like the other Microsoft networking services in Windows XP.  Progress, and a nod to Windows users that Tomcat and Java (which uses the registry now and does some other "Windowsy" stuff) are making sure that they work with Windows just as well as they work on UNIX and Linux.  A good sign for Java people.  Write once, Run anywhere.  Learn it.  Know it.  Live it.

 

What Does Tomcat Do?

Back in the old days, when you wanted to have an application on the World Wide Web you needed to jump through a whole bunch of hoops.  You had to worry about security, memory management, garbage collection, all those things you have to worry about when you write network programs.  When you write a program for the Web you used to write CGI scripts that did all of the processing and work.  In order to do anything big or complicated you had to write A LOT of code and it was often a mess and was often not very secure.

Then SUN came up with the concept of "web containers", programs that ran as services and were made to take care of all the dirty work of web applications while you worried about the code that had to do with the program logic.  The "Web Container" would serve the pages and process the incoming information in a safe and secure fashion, worrying about things like memory management and garbage collection along the way.  Tomcat is a such a Web container.  You could go so far as to say that Tomcat is "the" web container, because it's the one that SUN specs, recommends and does all its testing for. 

Because Tomcat ran server-side, a web application was a small program called a "servlet" that worked in conjunction with Tomcat to do the business of the program.  Tomcat also serves Java Server Pages and does the processing for them as well. 

The inner workings of the Tomcat server/container gets a little complicated, but the basics are this.  When you have a web page send data to the tomcat server it puts it in an object of type HttpServletRequest (usually called request or req).  When your servlet runs you inherit from HttpServlet and override a method called doPost() (That's if you used the POST method in your HTTP.  If you used GET you can define the doGet() method instead.  Most people have their doGet() call doPost() and do all their work in the doPost() method)

Once in the doPost() method you can use a variety of commands to extract the data from the request object.  You can do all of the processing you want and then write your output to another object, called response (this is of type... you guessed it... HttpServletResponse)  This object contains the HTML that will be returned to the user at the end of the process.

That was an extremely simple explanation, but that's the basics.  You will learn about things like the Model-View-Controller framework and when to use Servlets versus when to use JSP as you work with them. 

Just remember, Tomcat is the web container that does the dirty work.  You learn how to use it, what it expects, and how to extract the data your users send you and you will see that writing applications for the web is as simple/hard as it is for a regular client.  Tomcat goes much further, allowing you to tie in backend databases with realms, providing session tracking and doing a whole slew of other helpful things.  The bottom section will show you what books to get to get going with Tomcat. but first I'm going to show you a plug-in for Eclipse that makes using Tomcat even easier, Sysdeo...

 

Sysdeo

Now for a piece that puts everything together.  sysdeo is a plugin for Eclipse.  A plugin is just a small program that adds to the functionality of something.  Some give you extra buttons to do extra features, or color text, a wide range of different things.  Eclipse is made to be extensible, so it's easy to create a plugin for it.  Sysdeo is the plugin that integrates Tomcat with the Eclipse interface.  It gives you buttons that let you start, stop and restart your tomcat server, and most importantly, it sets up the servlet or JSP infrastructure that you need to have in order to get a Tomcat web project working correctly. Sysdeo is truly fantastic.  If you've ever done servlets the old way and then switch to Sysdeo you will see.  If you are doing Web projects with Tomcat for the first time then you are lucky you have it.

The picture above tells the story for Sysdeo.  Go to www.sysdeo.com/eclipse/tomcatplugin and grab the tomcatPluginV31beta.zip.  (right click, save target as...)

 

Unzip it so the base dir (com.sysdeo.eclipse.tomcat.3.1.0.beta) resides under the eclipse plugins directory (Should be C:\Program Files\eclipse\plugins).  There is no install.  Just unzip it in the right place.  Piece of cake.  Now just restart eclipse. 

 

You'll see those little Tomcat buttons in your tool bar (see the toolbar above) and you'll see that you have a Tomcat menu. 

 

The most important part is that when you start a new project you can open up the "Java" branch and create a "Tomcat project".  That's what you want to do.  The database project that Dr. Smith demoed for you is an Eclipse Tomcat project.  So, in eclipse, go to File->New->Project and click. 

 

Make sure you open up the "Java" folder and you'll see that the bottom selection is "Tomcat Project"

 

All you have to do now is name the project...  I called this sysdeotest...

 

 

And Viola.  Sysdeo has created the entire Tomcat web app infrastructure for me.  All I have to do is put my source code for my servlet in a class in WEB-INF/src, put my HTML in the base project dir (sysdeotest) and the web.xml under WEB-INF. 

 

NOW... We're not done yet....

You also need to go to "Windows->Preferences" in Eclipse and highlight the Tomcat branch at the bottom.  Fill in the details for "Tomcat home", click on the Server.xml button, and go to the Configuration file text box and type in "C:\Program Files\Apache Software Foundation\Tomcat5.5\conf\server.xml".  Good.  Now we look at the sub branches.  Click the plus sign to open up the branches.

 

 Go to Advanced and fill in the base dir : C:\Program Files\Apache Software Foundation\Tomcat5.5 

If you only have one JVM then you don't have to bother with JVM Settings, but if you have more than one make sure that you are using the same JVM in Eclipse that you are using with Tomcat.  It matters.  Actually, it matters big time, so make sure they're the same.

Go to Source Path and click both of the check boxes you see, the "Automatically computer source path" box and the box in front of your project name. 

 

Now, go to Tomcat Manager App.  This is where you need the password you typed in during the Tomcat install.  It allows Eclipse to change some settings in Tomcat so your app will run. (It adds a line to servlet.xml and tells Tomcat not to look in the webapp directory, but to look in the working directory of your eclipse Tomcat application.  This way your development environment is also your deployment site and you don't have to copy files back and forth.)  The password is out in the open, which is kinda weird, but what are ya gonna do?  You did a local install, so the Manager App URL should be the same for you as it is above.  The username will be admin and the password will be what you put in during the install of Tomcat.  If you want to tinker with Tomcat you can add users in the tomcat-users.xml file that resides in TOMCAT_HOME/conf.  That's for later if you want.

That should be it.  Now you're set up to use Sysdeo to create Tomcat applications.  Once you learn enough about Tomcat you'll see that you can build and test apps in your local environment with Eclipse and Sysdeo and then move what you have to a real server (Like Romulus. I know it's only version 4, and I know it only cycles every hour, and YES we know about redeploy on the fly and we can't do it for a more than a few valid  reasons that I don't have time to go into, but at least it serves apps to the real world, right?  test like mad until you're perfect and then put it up on Romulus for the world to see) for it to be deployed to the world.

If you've ever worked with Tomcat before you will LOVE Sysdeo.  It's really swift...

Books & Links

Three books will give you what you need to get started with Tomcat and Servlets & JSP.

Tomcat : The Definitive Guide - The O'Reilly Bible on the subject

Head First Servlets & JSP - Head First Java book.  Good for WCD certificate and general Servlet Info...

Core Servlets and Java Server Pages - The juice from SUN Microsystem, Inc.

Work through these three books and you'll know your stuff.

If you have any questions, suggestions, or corrections please let me know... 

biglars@cs.rutgers.edu

© 2007 Rutgers University