Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Tomcat 6 Developer's Guide
Tomcat 6 Developer's Guide

Tomcat 6 Developer's Guide: Understanding how a servlet container actually works will add greatly to your Java EE web programming skills, and this comprehensive guide to Tomcat is the perfect starting point.

eBook
€8.99 €28.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Tomcat 6 Developer's Guide

Chapter 1. Introduction to Tomcat

It is hard to overemphasize the importance of Apache Tomcat in the realm of Java Enterprise Edition web development. Tomcat began its life as source code that was donated by Sun Microsystems to the Apache Software Foundation (ASF). Since then, it has played a central role in the development and validation of the Java Servlet and JavaServer Pages specifications and has been the official reference implementation for these specifications.s

Its early popularity stemmed from the fact that it was easily integrated with two of the world's most popular web server software, Apache httpd and Microsoft's Internet Information Services (IIS) server.

However, since then, it has evolved into a production-grade product used by multiple large corporations (see a partial list at http://wiki.apache.org/tomcat/PoweredBy).

Tomcat is also found embedded, in part or whole, in various open source application server offerings. Even when using competing commercial application server offerings such as IBM's WebSphere and BEA's WebLogic, developers often use Tomcat to validate the vendor-independence of their applications.

It is also priced just right. It is open source and distributed under the Apache license, meaning it is free for use and modification (see www.apache.org/licenses for details). This also has the side effect of having the source code readily available for examination.

This availability of source code, strict adherence to specifications, and proven track record, combine to make Tomcat a unique learning opportunity for us. This book will assist you in taking the covers off this unique project, so that you get to see exactly how a production-grade servlet container is implemented.

What's the story behind Tomcat?

As I write this, the most current version of Tomcat is 6.0.20, where 6 is the major version, 0 is the minor version, and 20 is the bug fix release number. This version implements, and is completely compliant with, version 2.5 of the Java Servlet specification, and version 2.1 of the JavaServer Pages (JSP) specification.

That's a pretty hefty mouthful, so let's start at the beginning.

Every superhero has an origins story, and Tomcat is no exception. The Tomcat web site lists the available versions at http://tomcat.apache.org/whichversion.html, which shows the first stable release listed as Tomcat 3.x. So what happened to versions 1 and 2?

Tomcat was conceived in November 1998 by James Duncan Davidson at Sun Microsystems, who wrote it to form the core of the JavaServer Web Development Kit (JSWDK) for the Servlet 2.1 specification.

The name 'Tomcat' came to him when he was trying to decide a package name for the code he was working on. He ended up using com.sun.tomcat.

At the time, Apache JServ, a free servlet container, was in the process of being updated to support the Servlet 2.1 specification.

However, this effort was abandoned due to the donation of Tomcat by Sun Microsystems to the Apache Software Foundation, at JavaOne in 1999. It soon came to life as Tomcat version 3.0, the successor to JSWDK 2.1.

This was a major milestone for the open source community as it meant that a commercial package owned and developed by Sun Microsystems, and which implemented the latest Servlet 2.2 and JSP 1.1 specifications, was now being offered to the general public for free.

All of this made the decision to abandon Apache JServ rather uncontroversial.

The focus soon shifted from updating JServ to support the new specification, to getting Tomcat to implement features that were missing. The most important one being the ability to cooperate with the Apache web server. This was critical to the popularity of Tomcat, as it meant that developers could leverage the static file handling capabilities of the Apache web server together with the dynamic capabilities of Tomcat to achieve a well rounded solution. Subsequent Tomcat releases have actually strengthened this connection component.

In addition, Tomcat can now directly serve static content and so is often deployed in standalone mode without a separate web server.

It is interesting to note that Tomcat began life not as its own top level project. Instead, it started humbly as a subproject within the Jakarta project (an umbrella project that covers many subprojects such as Apache Commons, Cactus, and JMeter). It was only in 2005 that it was upgraded to the status of a top level project at Apache.

What's the story behind Tomcat?


As I write this, the most current version of Tomcat is 6.0.20, where 6 is the major version, 0 is the minor version, and 20 is the bug fix release number. This version implements, and is completely compliant with, version 2.5 of the Java Servlet specification, and version 2.1 of the JavaServer Pages (JSP) specification.

That's a pretty hefty mouthful, so let's start at the beginning.

Every superhero has an origins story, and Tomcat is no exception. The Tomcat web site lists the available versions at http://tomcat.apache.org/whichversion.html, which shows the first stable release listed as Tomcat 3.x. So what happened to versions 1 and 2?

Tomcat was conceived in November 1998 by James Duncan Davidson at Sun Microsystems, who wrote it to form the core of the JavaServer Web Development Kit (JSWDK) for the Servlet 2.1 specification.

The name 'Tomcat' came to him when he was trying to decide a package name for the code he was working on. He ended up using com.sun.tomcat.

At the time, Apache JServ, a free servlet container, was in the process of being updated to support the Servlet 2.1 specification.

However, this effort was abandoned due to the donation of Tomcat by Sun Microsystems to the Apache Software Foundation, at JavaOne in 1999. It soon came to life as Tomcat version 3.0, the successor to JSWDK 2.1.

This was a major milestone for the open source community as it meant that a commercial package owned and developed by Sun Microsystems, and which implemented the latest Servlet 2.2 and JSP 1.1 specifications, was now being offered to the general public for free.

All of this made the decision to abandon Apache JServ rather uncontroversial.

The focus soon shifted from updating JServ to support the new specification, to getting Tomcat to implement features that were missing. The most important one being the ability to cooperate with the Apache web server. This was critical to the popularity of Tomcat, as it meant that developers could leverage the static file handling capabilities of the Apache web server together with the dynamic capabilities of Tomcat to achieve a well rounded solution. Subsequent Tomcat releases have actually strengthened this connection component.

In addition, Tomcat can now directly serve static content and so is often deployed in standalone mode without a separate web server.

It is interesting to note that Tomcat began life not as its own top level project. Instead, it started humbly as a subproject within the Jakarta project (an umbrella project that covers many subprojects such as Apache Commons, Cactus, and JMeter). It was only in 2005 that it was upgraded to the status of a top level project at Apache.

What exactly is Tomcat?


For most users, the typical web interaction can be represented as follows: Joe User enters a Uniform Resource Locator(URL) in the browser's location box to request the resource that is identified by that URL. A URL is composed of a host name, an optional port (which defaults to 80), and the resource being requested.

When a web server receives that request, it first determines whether the resource being requested is static or dynamic in nature.

A static resource is one that is identical no matter how many times, or in what order, that resource is requested. Some examples of these would be a company's logo on a web page, a CSS style sheet, or a typical HTML page.

The web server handles the request for a static resource by simply fetching the identified resource from the appropriate path in which its static content resides (for example, htdocs for Apache), and returning those bytes in the response to the user.

For static resources, a depiction of this request flow is as shown below.

A dynamic resource, on the other hand, indicates a resource that varies its content based on various factors that may include the specific user making the request, the time of day when the request is made, or the presence of additional information in the request that affects how the request is interpreted. Some examples of these would include the current inventory for a specific product in an online store, or the availability of a seat on an airplane.

Most web servers are tuned to deliver blazing fast request handling for static resources. However, they require additional help when dealing with dynamic resources. This help is typically provided by an additional software component that is registered with the web server, called a servlet container on the Java EE platform.

In the IIS world, access to this servlet container is offered by implementing an Internet Server Application Programming Interface (ISAPI) extension, which is a Dynamic Link Library (DLL) that allows you to extend the functionality of IIS. In the Apache world, you access the container using code written in C or C++.

When a request comes in that is directed at a dynamic resource, the web server simply hands off that request to its associated servlet container.

The container then springs into action, invokes the appropriate servlet that represents the requested dynamic resource. This servlet is responsible for generating the response. The generated response is returned by the servlet container to the web server which, in turn, returns it to the requesting user. This interaction is depicted in the following diagram.

A servlet in its most basic form is a Java class that acts as a dynamic web resource. In other words, it can be the target of a client browser's request.

Being a Java class, it can tailor its response according to the payload of the incoming request, the conversation that this request is a part of, as well as other environmental criteria (such as the time of the day or the inventory status from a database).

Note

It is important to note that there is nothing special about this delegation mechanism that restricts it only to dynamic resources. You could configure a web server to also delegate requests for a certain subset of static resources (for example, those within a given URL path) to the servlet container.

JSPs

The problem with using Java code to generate HTML content is that you end up with a lot of string generation and concatenation to generate the actual HTML content, interspersed with actual program logic written in Java. This is an unholy combination as it tends to make the program logic impervious to the person tasked with maintaining your code. In addition, changes to static content now require you to compile the servlet class.

Applying the 80-20 rule very loosely to dynamic content, you might find that 80% of the content of a page is in reality static content. The JavaServer Pages specification was evolved as the solution to this situation. A JSP (JavaServer Page) is a template made up primarily of static content with very specific (and hopefully, few) invocations of Java code to retrieve the dynamic aspects of the page.

The problem of requiring a compilation step was resolved by making a JSP an artificial construct. The container is responsible for transparently parsing a JSP and converting it into a bona fide servlet, compiling it, and then invoking it in the same manner as the other servlets in the application.

As long as the ratio of static to dynamic content is high, that is, as long as there are more static elements than dynamic elements on a page, even non programmers, such as graphic artists, can find their way around a JSP file with some comfort.

This is about as much as we will cover on JavaServer Pages technology in this book.

Servlet container

With all that behind us, we are now ready to answer the question posed by this section's title-what exactly is Tomcat?

Tomcat is classified as a servlet container, that is, an environment within which servlets can live and prosper. As a container, it provides a lot of administrative support to servlets, allowing programmers to focus on the core application logic that is to be implemented, without having to bother about low level specifics such as session management and class loading.

The servlet and JSP specifications describe the service contract that the container promises to provide to the servlets and JSP files that we write. Together, the specifications describe all the services that a container should provide, and specify how the servlet may make use of those services. As with all other Application Programming Interfaces (APIs), the specifications describe the 'what' and leave the 'how' to the implementer of the specific container.

Tomcat serves as the 'reference implementation' of these specifications, and as a result, serves as a guinea pig or canary in the mine shaft for them. In other words, it provides a sample implementation to prove that the specification can indeed be implemented, and serves as a guide for other implementers.

The following diagram provides an early bird's eye view of the responsibilities of a servlet container.

The primary responsibility of a container is to process an incoming request and to generate a corresponding response that is then returned to the client.

A Connector component provides the external interface that allows clients to connect to the container. This component not only accepts incoming connections, but is also responsible for delegating the processing of the request to an available request processor thread. The 'Processors' block in the image denotes multiple threads in a pool that may be used to process the incoming request.

The request processing framework is implemented using a multiple level hierarchy of sub containers (the servlet Engine, virtual hosts, web application contexts, and servlet Wrappers) and nested components (Valves, Listeners, Loaders, Resources, and Security Realms). Note that the '+' sign in the above image indicates that there can be more than one instance of that component. For instance, more than one Host, Context, and servlet Wrapper component may be contained within its parent container.

The result of processing a request is represented by a response, which is then returned through the same Connector.

In addition to the core request processing components, the container also provides support for other aspects of operation, such as logging, security, and JMX monitoring.

In this book, our primary focus will be on the request processing responsibilities of a servlet container. These components form the unshaded boxes in the previous image. Additional topics will be considered only when they are crucial to understanding a request processing component's actions.

Note

It is important to note that a servlet container is a completely different beast from an application server.

In the simplest sense, an application server contains a servlet container, along with other containers, such as an EJB container, an applet container, and an application client container. In addition, an application server provides implementations for a number of Service APIs that are required for heavy duty applications. We will take a closer look at the Java EE platform in Chapter 2.

Examples of application servers include the JBoss Application Server, GlassFish, IBM's WebSphere, and BEA's WebLogic.

Whether or not you should use a full fledged application server, with its support for Enterprise JavaBeans and other Java EE heavy artillery, depends on your application needs.

Why this book?


Tomcat is one of the few technology areas that is particularly blessed with a plethora of very well written books, and the Tomcat mailing lists are rather friendly places for new developers. So really, why another book on Tomcat?

The answer is simple. Most of these books and material focus primarily on the set up, administration, and management, of a Tomcat server.

This is no accident. Most web developers rarely have to concern themselves with anything more challenging than setting up a context fragment or changing the default port for Tomcat from 8080 to 80.

As long as a developer understands the basic terminology of web applications (such as the context, the context root) and the structure of a WAR file, most modern Integrated Development Environments (IDEs), such as Eclipse and IntelliJ IDEA, do all the heavy lifting for you.

It seems only appropriate then, for authors to target their books at the typical administrator or deployer.

For its part, Tomcat exposes a myriad of configuration files that enable even an obsessive administrator to fine tune the installation and the Java Virtual Machine as much as desired.

The net result is that your average book contains the customary chapter on installing Tomcat, chapters on configuring it, on deploying servlets into it, on integration with either the Apache httpd web server or Microsoft's IIS, and on securing the installation.

The downside with this approach is that it provides a one-sided view of the software engineering marvel that is Tomcat. You tend to see it as a black box with lots of control switches that you can throw, but you never ever really get a chance to peer into the open vents on the sides of that box.

In this book, I hope to correct that imbalance. I'm going to pry open the service door on the side of that black box, and take you in on an interesting journey through some of the sights that most people rarely get to see. A connector shimmering in the moonlight, the rays of the early morning sun catching an application class loader in its native state, a lumbering valve caught in motion capture frames as it springs into action with surprising agility. There are a lot of fantastical beings that populate this world, if you only dare to venture into it.

In the course of the next few chapters, we will look at Tomcat from the perspective of someone attempting to understand what makes it tick. Our goal will be to understand Tomcat as few others do. We'll not only see the control switches on the outside, but we'll also learn about how these switches affect the machinery within.

Gathering our tools


In this section, I'm going to get together all the pieces that you will need for the remainder of this book. If you would like to follow along with the rest of this book, then this section is crucial.

The software that we will download and install includes the following:

  • The Java SE Development Kit (JDK) version 6.0

  • A download verifier

  • Apache Ant 1.7.1

  • Subversion 1.6.3

  • Eclipse 3.5 Galileo

The workstation that I use throughout this book runs a Microsoft Windows operating system (XP Professional or Vista).

Note

The version numbers that are specified were current at the time of writing this text. However, with the speed of product cycles in the open source arena, it is likely that newer versions will be available by the time you read this.

Java Development Kit (JDK) 6.0

JDK 6 can be found at http://java.sun.com/javase/downloads/index.jsp.

As with every download page on the Sun web site, there are enough choices to make your head spin. For our needs, we need the plain vanilla JDK 6.0 that at the time of writing is JDK 6 Update 14.

Once you've picked the Platform: (Windows) and Language: (Multi-language), you will end up with a file named jdk-6u14-windows-i586.exe on your workstation.

Execute this file to install it. Once you have accepted the terms and conditions, and have picked an install directory, the installation process will begin.

Once the JDK has been installed, the installer will prompt you to install the JRE. You may do so if you wish.

Note

The JDK for 64-bit platforms

There are 64-bit versions of the JDK that are available for Windows, Linux, and Solaris. The file for 64-bit Windows is named jdk-6u14-windows-x64.exe.

Verifying downloaded files

Most open source software is hosted on multiple mirror sites, and you are expected to pick your nearest mirror from which to download it. A key question here is how do you ensure that the file that you just downloaded is an exact copy of the version that the open source team intended for download, and there isn't a malicious hacker chuckling with glee to see you install a Trojan on your workstation?

There are a couple of answers to this question—using PGP keys or using a message digest.

The former is more secure as it uses public key cryptography, but as a result is also more complex.

The latter, while simpler to use, is not quite as secure, as the most common digest algorithm (MD5) has some well documented flaws, allowing different files to generate the same digest. In addition, the MD5 digest value itself may have been modified by the hacker so that it matches the compromised file.

Using the principle that any security is better than none, you would compute the MD5 message digest for the downloaded file, and compare that computed value against the published digest for the file.

You can download a digest generator from either www.formilab.ch/md5/ or www.nullriver.com/index/products/winmd5sum.

For example, the MD5 digest for the ZIP file distribution of Tomcat 6.0.18 is:

f7d0c15df19fedf52476767f7ce0b6f8

*apache-tomcat-6.0.18.zip

Apache Ant

Apache Ant or Another Neat Tool is a build automation utility that owes its existence to James Duncan Davidson and Tomcat. It originated as a tool used to build Tomcat itself. But its widespread popularity ensured that it was promoted into its own project, independent of Tomcat, in January 2000.

As Ant is written in Java, it inherits its Write Once Run Anywhere aspect, making it platform independent. Its build directives (by default, in a file named build.xml) are written using XML - another cross platform advantage.

Ant build directives take the form of targets that describe the actions or tasks that must be performed, whether creating, deleting, or copying files and directories, setting classpaths, compiling Java classes, or even packaging your applications.

The ability to define dependencies on targets adds to its flexibility. You can indicate that a compile target depends on other targets that clear out the output directories, fetch the latest changes from your source code repository, and set the classpath.

Finally, throw in the ability to define your own targets, and the sky's the limit!

Download the latest version from http://ant.apache.org/bindownload.cgi and extract the Ant distribution. The version that was current when I wrote this, was Ant 1.7.1.

You need to add Ant to your PATH. On Windows, this is accomplished by opening the System Properties box - by right clicking your My Computer icon and clicking Properties. Pick System from the Control Panel's classic view or press the keyboard shortcut Windows + Pause/Break.

On the Advanced system settings tab, click on Environment Variables tab and edit the Path variable. Add [ANT_HOME]\bin to the end of the path variable, replacing [ANT_HOME] with the absolute path to the location where you extracted the downloaded file.

Note that path variables in Windows are delimited by a semicolon.

Subversion

Next, we need a way to retrieve the latest version of the Tomcat source code, which is currently housed in a Subversion repository.

Subversion was conceived in 2000, when CollabNet, Inc. decided that it was time to write a brand new version control system, and brought on Karl Fogel to head this effort. The stated goal of the original team was to write a better version of CVS, while retaining the familiar elements of CVS, to make it easier for developers to switch without much effort.

Subversion has since dethroned CVS as the system of choice. In particular, it has been adopted as the standard version control mechanism by the Apache Software Foundation.

Subversion, like CVS, uses a copy-modify-merge model. In the copy phase, each developer makes a private copy of the project's source code on to their individual workstations. This copy acts as a sandbox within which each developer may work.

In the modify phase, each developer makes changes in his or her own working copy. The changes made by one developer are not visible to the others. If you need visibility into another developer's changes, you would need to update your working copy from the repository.

In the merge phase, a developer commits his changes back into the source code. If a file was modified by another developer since you checked out your copy, you would then need to merge your changes back into the repository resolving any conflicts as necessary.

Get the latest version of the Subversion Command-Line Client for Windows from http://www.collab.net/downloads/subversion/, which is currently 1.6.3. Execute the downloaded file, CollabNetSubversion-client-1.6.3-3.win32.exe.

The installation is fairly straightforward and only requires that you specify a path name for your installation.

To verify that the software was successfully installed, run the svn --version command from the command prompt. This command should output the version of Subversion that is installed on your workstation.

Obtaining the Tomcat source code

You have two options when trying to download the Tomcat source. You could either use the Subversion version control software, which was described in the last section, or you could simply download the compressed source distribution of Tomcat as a ZIP file. The former option is preferred if you expect to be developing in Tomcat, whereas the latter is simply a snapshot of the code at a given version.

Using Subversion

To obtain the latest source code for Tomcat from the Subversion repository at the ASF, change your working directory to where you want to download your working copy (for example, c:\tomcat), and execute the following at the command line (all on one line):

 svn checkout http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/

When this completes, you will notice that your current directory now contains a trunk subfolder with the source directories for Tomcat.

Note

The above command downloads the most up-to-date version of the source trunk and may result in code that may differ from the descriptions in this text.

If you would like to ensure that you are working with the exact codebase covered in this book, then you should use the following command instead:

svn checkout http://svn.apache.org/repos/asf/tomcat/tc6.0.x/tags/TOMCAT_6_0_20/

This is the 'copy' phase of your interaction with the Subversion version control system. The result of this operation is to make a local copy of Tomcat's latest development code line from the Subversion repository. This lets you play in your own little sandbox, making changes and experimenting for as long as you need.

Tomcat source distribution

If you'd rather not install Subversion, a less invasive option would be to simply download the Tomcat source from the Source Code Distributions section at http://tomcat.apache.org/download-60.cgi.The source for a previous version can be downloaded from http://archive.apache.org/dist/tomcat/tomcat-6/. The only downside with this approach is that the source distribution does not contain a project file for use with the Eclipse IDE. To get these files, point your browser at http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk (or to http://svn.apache.org/repos/asf/tomcat/tc6.0.x/tags/TOMCAT_6_0_20 for the version used in this book) and download the .classpath and .project files. Ensure that the downloaded files are named exactly the same as their originals.

Getting ready to build Tomcat

Now, we're ready to get started with building the source that we just downloaded. This is a two step process that utilizes the power of Ant and the build.xml file that exists in the trunk directory. First change directory to the directory to which your source code has been downloaded (or unzipped.).

Downloading dependencies

Now, run the download target:

ant download

The download target downloads a number of binaries that are needed for this build. These artifacts are downloaded to the folder that is identified by the base.path property in the Ant build.properties.default file. By default, this will be /usr/share/java. However, you can change this to a different absolute path such as c:/tomcat/downloads/trunk.

The files that are downloaded include:

  • The native Tomcat connectors (currently at version 1.1.15).

  • The Commons daemon library (version 1.0.1), which is used with jsvc—a Java Service Wrapper that is used to run Tomcat as a non-root user listening on the privileged port 80 on non Windows operating systems.

  • The Eclipse JDT JAR, which is used by Tomcat to compile JSPs.

  • The Nullsoft Scriptable Install System (NSIS)—an open source system to create Windows installers.

  • The Commons Collections library.

  • The Commons Database Connection Pool (version 1.2.2) and Commons Pool (version 1.4). The build-tomcat-dbcp Ant target renames the packages for these Commons projects, for example, by replacing org.apache.commons with org.apache.tomcat.dbcp. The stated goal here is to ensure that the DBCP and Pool classes, used by Tomcat internally, do not conflict with any versions of DBCP or Pool classes that are packaged with individual web applications.

Note

Download errors

I have noticed that the download target sometimes fails when trying to unzip the NSIS with the following error: java.io.IOException: Negative seek offset

In such cases, a manual workaround is to simply download the target ZIP file (in this case http://downloads.sourceforge.net/nsis/nsis-2.37.zip) and unzip it to the folder into which the build script is downloading files. Then, ensure that the test file is present, which in this case is c:/tomcat/downloads/trunk/nsis-2.37/makensis.exe.

Building Tomcat

We're finally ready to build Tomcat using Ant, so run the deploy target.

ant deploy

This target compiles the Java code and builds the binary distribution of Tomcat. Note that you can omit the target name because deploy is defined as the default target in the build.xml file. So the following would be equivalent:

ant

The deploy target builds the Tomcat binaries and copies files out to output\build.

Note

The "clean" target

If you need to start with a clean build, then the clean target is your friend. It clears out all the output folders for a fresh distribution to be laid into it.

Starting up your shiny new Tomcat

From the output\build\bin directory within your Tomcat trunk folder, execute the startup.bat batch file.

startup

This starts up your Tomcat server in a separate command window. Reassuring log messages should be output to your console as shown.

Now, using your favorite browser, type in the URL http://localhost:8080, and marvel at what you have wrought.

Finally, to shut down your server, execute the shutdown.bat batch file.

shutdown

This command closes down the Tomcat server gracefully, and you're done.

Eclipse

We're now on to the home stretch. The ultimate prize for us is to be able to use an IDE to run the Tomcat server. What would be more appropriate for investigating Tomcat, than the free and full featured open source IDE—Eclipse!

Note

While you can simply use your favorite IDE, the Tomcat source distributions include project metadata files only for the Eclipse IDE.

In particular, let's pick Eclipse Galileo, which is the most recent in an annual series of releases that package multiple major projects. Previous releases have been code named Callisto, Europa, and Ganymede.

Galileo was released in June 2009, and includes the core Eclipse project along with almost three dozen other major projects, including the Web Tools Platform project, the Rich Ajax Platform project, and the SOA Tools Platform project.

Eclipse downloads are available at http://www.eclipse.org/downloads/. Make sure that you pick the Eclipse IDE for Java EE Developers version. For Windows, this results in the file eclipse-jee-galileo-win32.zip being copied down to your workstation.

Note

Eclipse for 64-bit Windows

The above link works fine for downloading Eclipse for the most common platforms, including 64-bit Linux. However, you have to work a bit harder to get the 64-bit version for Windows. The downloads for the Eclipse project are available at http://download.eclipse.org/eclipse/downloads/. For safety, click the link associated with the Latest Release version, which is currently at 3.5. These downloads are named eclipse-SDK-[versionNumber]-[platform].zip. For Vista 64, this download is eclipse-SDK-3.5-win32-x86_64.zip.

Unzip the Eclipse distribution to a folder of your choice, and execute eclipse.exe to verify that your IDE starts up.

At startup, you are prompted for a workspace location. A workspace folder is used to house your projects as well as to store metadata about your plugins and your IDE preferences.

In this dialog, you can either point to an existing workspace folder if you have one, or to a new folder location. It is recommended that you pick a folder that lies outside of your Eclipse installation directory so that when you upgrade your Eclipse version, you can simply delete the old installation folder without losing your workspace.

Once Eclipse starts up, dismiss the Welcome screen to proceed to the Workbench, and then select the Window | Preferences menu option to display the Preferences dialog.

In the left pane of this dialog, drill down to Java | Installed JREs, and verify that your previously installed version of JDK 6 shows up. If it doesn't, then click on the Add button to add a reference to your JDK.

We will also set up two classpath variables as shown next— ANT_HOME to point at the base directory for your Ant installation and TOMCAT_LIBS_BASE to point to the directory that holds the files downloaded by the download ant target that you executed while building Tomcat.

Note

If you are building a non-trunk version of Tomcat, then ensure that your TOMCAT_LIBS_BASE classpath variable points to the path to where the ant download target downloads its dependencies.

Next, import the project file that is present in the working copy of the Tomcat trunk. Choose File | Import to open the Import dialog. From this dialog, choose General | Existing Projects into Workspace to import the Eclipse project present in the Tomcat trunk into your new workspace.

Click on Finish to import the project.

A project build begins automatically, and you will get the following screen.

To start up your Tomcat server from within Eclipse, create a Run configuration by choosing the Run | Run Configurations… menu option, and select org.apache.catalina.startup.Bootstrap as the Main class.

Click on Run, and watch the Console pane for output indicating that Tomcat was successfully started.

Now browse on over to http://localhost:8080 to view the Tomcat welcome page.

Congratulations! You're done.

For now, play with this code by placing breakpoints and stepping through interesting code sections. We'll explore these sections in far greater detail in the chapters to come.

In the next couple of chapters, we'll regroup and visit concepts that are necessary for the exploring that we'll be doing in later chapters.

Summary


We covered a lot of ground in this chapter. We began by looking at the principles behind a servlet container. We looked at the origins of Tomcat, and we built ourselves our very own server by cobbling together a number of software helpers.

We looked at the JDK, the Ant build tool, the Subversion version control tool, and the Eclipse IDE. We retrieved the latest source from the Tomcat Subversion repository, and we compiled the Java sources to result in a binary distribution of the container.

In the next chapter, we'll explore some of the ideas embodied in the servlet specification.

Left arrow icon Right arrow icon

Key benefits

  • Take your Java EE web programming skills to the next level by getting an expert's level understanding of the servlet specification and its reference implementation, Apache Tomcat.
  • Build a Tomcat distribution from its source code, and explore the components, classes, and technologies that make up this container.
  • Use standard development tools such as Eclipse, Ant, and Subversion to dissect a Tomcat distribution.
  • Discover the touch points between the servlet specification and a servlet container's implementation.
  • Acquire specialist grade skills in a range of technologies that contribute to Java server side development

Description

While Tomcat is one of the most popular servlet containers, its inner workings still remain a mystery to many developers. If you only have a superficial familiarity of how this container actually functions, much of its power remains untapped and underutilized. This book will provide you with all that you need to undertand how to effectively use Apache Tomcat. This book begins by providing detailed instructions on building a Tomcat distribution. The next few chapters introduce you to the conceptual underpinnings of web servers, the Java EE and servlet specifications, and the Tomcat container. Subsequent chapters address the key Tomcat components, taking care to provide you with the information needed to understand the internal workings of each component. Detailed examples let you walk through a Tomcat installation, stepping into key Tomcat components, as well as into your own custom servlets. During the course of the book you will encounter various structural components such as the Server and Service; containers such as the Engine, Host, Context, and Wrapper; and helpers such as the Loader, Manager, and Valve. You will also see how Tomcat implements the JNDI API to provide both a directory service for storage agnostic access to its resources, as well as a naming service that implements the Java EE Environment Naming Context. Along the way you will learn how various elements of the servlet 2.5 specification, as well as the HTTP RFCs are implemented by a servlet container. By the end of your journey, you will be able to count yourself as part of the elite minority of Java EE web developers who truly understand what goes on under the covers of a servlet container.

Who is this book for?

This book is intended for web developers who want to take their programming skills to the next level, and for server administrators who want to get a better understanding of the container software they manage. This book will give you an expert's understanding of every component that contributes to the overall request processing capabilities of Tomcat. In addition, the concepts you learn will be general enough to be carried over to other servlet containers. This book is also a good starting point for developers who want to understand how high availability and high performance software, such as Tomcat, are designed. If you wish to contribute to the Tomcat project, then this is an ideal book for you. Prior knowledge of the Java programming language, and web development on the Java platform is assumed.

What you will learn

  • Obtain an in-depth view of the inner workings of one of the most successful open source projects of all time.
  • Discover the touch points between the Java EE platform specifications and a servlet container s implementation.
  • Learn how to extract the most out of your Tomcat installation by understanding how its various components are implemented.
  • Use standard development tools such as Subversion, and Ant, and Eclipse to build a Tomcat distribution from its source code.
  • Execute Tomcat from inside the Eclipse IDE to watch the various components cooperate in processing a request.
  • Acquire specialist grade skills in a range of technologies and protocols that contribute to Java server side development.
  • Learn advanced software techniques such as building a custom class loader, implementing a custom protocol, and implementing JNDI-based naming and directory services.
  • Explore some of the performance optimizations and design patterns used in building highly scalable and performant software.
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 10, 2009
Length: 416 pages
Edition : 1st
Language : English
ISBN-13 : 9781847197283
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Portugal

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Dec 10, 2009
Length: 416 pages
Edition : 1st
Language : English
ISBN-13 : 9781847197283
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 112.97
Apache Tomcat 7 Essentials
€32.99
Tomcat 6 Developer's Guide
€37.99
Object-Oriented JavaScript - Second Edition
€41.99
Total 112.97 Stars icon
Banner background image

Table of Contents

11 Chapters
Introduction to Tomcat Chevron down icon Chevron up icon
Servlet API Overview Chevron down icon Chevron up icon
Servlet Container Overview Chevron down icon Chevron up icon
Starting Up Tomcat Chevron down icon Chevron up icon
The Server and Service Components Chevron down icon Chevron up icon
The Connector Component Chevron down icon Chevron up icon
The Engine Component Chevron down icon Chevron up icon
The Host Component Chevron down icon Chevron up icon
The Context Component Chevron down icon Chevron up icon
The Wrapper Component Chevron down icon Chevron up icon
The Manager Component Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(3 Ratings)
5 star 33.3%
4 star 66.7%
3 star 0%
2 star 0%
1 star 0%
Mike Angstadt Nov 23, 2011
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is very wholistic in its approach to educating the reader. It provides a well-written overview of JavaEE and HTTP in Chapter 2. And whenever it mentions a technology that Tomcat uses in some way, it immediately gives a brief overview of that technology. I found this helpful because it gave me the knowledge I needed to continue reading without having to switch to a search engine. For example, in Chapter 5, when discussing the JNDI functionality that Tomcat comes packaged with, the book pauses for a moment to explain the basic concepts of JNDI itself. The book will also often describe the historical origins of the technology, like in Chapter 1, where it discusses the origins of Ant (spoiler alert: it was created as a custom build system for Tomcat).The instructions for downloading the Tomcat source code and building it are very clear. It provides a specific branch you can checkout from the Tomcat Subversion server, which is the exact version of Tomcat that was used when writing the book. This allows you to follow along without worrying about dealing with differences between what you see in the code and what you see in the book. And it also includes instructions for getting the project properly configured with Eclipse.The later chapters of the book go into a lot of detail about the various classes that make up the Tomcat source code, so be prepared for some serious code spelunking. Anyone who is interested in doing serious coding in the Tomcat codebase will gain a lot from these chapters.I'd recommend this book to people who want to work on the Tomcat source code and need something to guide them through it. I'd also recommend the book to JavaEE developers who use Tomcat on a regular basis and who want to expand their knowledge.
Amazon Verified review Amazon
I. Cherouvim Feb 15, 2010
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I've been writing Java webapps and using tomcat on production for the past 5 years.This book is the best and most complete tomcat analysis book I've read. It is for developers and sysadmins, and it gives both the high level and very detailed views of the system. It starts by taking you through building your own tomcat from source (using ant and eclipse) and extensively explains the servlet spec, JNDI and all parts of the container. It also unravels a very dark corner of java (which directly relates to web containers), the classloader, in a way which does make sense.The book shows all the configuration details, in a pragmatic way. Some of the options and usage patterns are not that well written on the official tomcat documentation site.While reading it I improved my tomcat configuration on production by reducing the memory footprint of the container. I also vastly improved my understanding on the servlet spec, web containers and tomcat itself so I had a chance to improve parts of my production code.The book is very well written and the only disadvantage is that at some parts you feel like the author steps through the code describing it in English. Fortunately these parts are few and short.All in all, the amount of knowledge this book gives is great and nowhere else to be found is such cohesive way.
Amazon Verified review Amazon
Mark Petrovic Nov 10, 2012
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
In the course of porting Jon Brisbin's excellent RabbitMQ-based Tomcat session replication manager from Tomcat v6 to Tomcat v7, I discovered Mr. Chetty's book.Here is the port: [...]While I was able to get the port to work without reading the book, I in fact read the book in parallel with the port. And in so doing I was able to acquire a deeper appreciation and understanding of Tomcat internals and how Tomcat actually works. The book is very well written, and the information is paid-out wisely to bring the reader along in their understanding.The book is based on Tomcat 6, which is not the latest major version. But that really doesn't matter. The basics of Tomcat remain the same, and the reader can fill in specific Tomcat7 information by reading the source code. Is the book perfect? Probably not. But as a glass half full person, this book easily gets the reader to the requisite 80% mark in their understanding of Tomcat internals.I recommend this book to any curious, serious developer of Java webapps that run on Tomcat. It's one of those book I expect to own for a long time, and is likely on the "loan with care" list so I don't lose track of who has it.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela