Chapter 1. Setup

To begin, you’ll need to set up the environment for developing and deploying the Ajax examples in this book. This environment is different from that used for many other technologies.

Requirements

To clarify, Ajax isn’t a language or a software package; there is no single source of Ajax technology. If you’re a Java developer, you probably already have many of the tools you need to work with Ajax.

Let’s review the minimum requirements that you will need to develop an Ajax application with Java:

Browser

You will need a browser that supports JavaScript (Internet Explorer, Safari, Mozilla, Opera, Firefox, etc.).

Java Development Kit

You will need a Java compiler and libraries, preferably for Java 5 or Java 6.

Apache Ant

You will need Apache Ant. You can get by without Ant, but only if you’re a masochist. (An alternative is Maven. The examples in this book assume you’re using Ant, but adapting them to Maven shouldn’t be difficult.)

Application server

The server piece can be any application server that can host Java servlets and can communicate via HTTP. The examples in this book have been developed using Sun’s JDK 1.5 and Apache Tomcat 5.0, but there are many other application servers (such as JRun, JBoss, Resin, WebLogic, WebSphere, and Glassfish) that you can use with Ajax.

If you are going to use a servlet container other than Tomcat, you can skip the "Installing Tomcat" section. However, I advise you to use Tomcat first; after you understand an example and have it running, then try it on a different server.

Installing Tomcat

Start by downloading and installing the latest released version of Tomcat (browse to http://jakarta.apache.org/tomcat/ and select the Current Releases link under the Downloads section). If you have never used Tomcat, you’re in for a pleasant surprise. Tomcat is a great servlet engine that is used as the reference for the Java Servlet and JavaServer Pages technologies.

Tomcat is free, and Tomcat is mature. If you get a released production version, you will find that it is as stable as any production-version commercial application server. The Tomcat project also has good documentation; take advantage of it. If you’re new to Tomcat, another good resource is Jason Brittain and Ian Darwin’s Tomcat: The Definitive Guide (O’Reilly).

A Minimalist Guide to Setting Up Tomcat

For Linux/Unix, download the tar.gz file and install it by running tar -zxvf in the directory where you want Tomcat to reside (e.g., /usr/local/tomcat). For Windows, Tomcat ships as a self-extracting executable: just download and run setup.exe to install it.

Once you’ve installed Tomcat, start it running on Linux or Unix with the following command:

/<tomcat install directory>/bin/startup.sh

On Windows, use the following command:

\<tomcat install directory>\bin\startup.bat

Then start up a browser and browse to http://localhost:8080 to see the Tomcat home page. From there you can run the example servlets to ensure that your installation is working correctly.

To shut down Tomcat, run the command shutdown.sh (Linux) or shutdown.bat (Windows) from your install directory.

Setting TOMCAT_HOME

All the examples in this book will be built and deployed with Ant. (If you’re not familiar with Ant and the concept of build files, you might want to take some time to familiarize yourself with them now.) The build files will require the TOMCAT_HOME environment variable to be set properly, to ensure that when you deploy your applications, build.xml will copy everything you need into the webapps directory of the Tomcat server.

To check the value of TOMCAT_HOME on a Windows machine, type set from a command prompt. Along with the other environment variables, you should see:

TOMCAT_HOME=c:\apps\Tomcat5.0

TOMCAT_HOME should be set to the location where you installed Tomcat. If it is not, set TOMCAT_HOME using the environment variables setup screen (Start → Control Panel → System Properties → Advanced → Environment Variables). If you don’t know how to do this, open Help from the Start menu and search for “environment variables.”

On Linux, from a command prompt type the command set | grep TOMCAT. You should see something like this:

TOMCAT_HOME=/usr/local/tomcat/Tomcat5.0

Again, the value of TOMCAT_HOME should be the directory where you installed Tomcat. If it isn’t, you need to set it correctly. Usually this requires adding an export command such as the following to a resource file like .bashrc:

export TOMCAT_HOME=/usr/local/tomcat/Tomcat5.0

Installing Ant

To run the examples in this book, you’ll also need to download and install the Ant project. Browse to http://ant.apache.org and grab the latest version.

Make sure that the bin directory of your Ant installation is in your path, and then type ant at the command prompt. Ant should come back with the message “Build file does not exist.” This message means that Ant is installed correctly and could not find the build.xml file when it tried to load it.

If you don’t have Ant installed correctly, you will see an error such as “executable file ant not found.” In this case, check to make sure that your PATH environment variable is set to include the bin directory of the Ant installation. As with TOMCAT_HOME, Windows users can set the PATH variable through System Properties (Start → Control Panel → System Properties → Advanced → Environment Variables), while Linux and Unix users must add lines such as the following to their shell’s initialization file (most likely .bashrc):

export TOMCAT_HOME=/usr/local/tomcat/Tomcat5.0
export PATH=$PATH:$TOMCAT_HOME/bin

Once you have Ant installed correctly, you can use it to build the applications presented in this book. If you need more information on Ant, consult the documentation at http://ant.apache.org or one of the many books written about Ant. Steve Holzner’s Ant: The Definitive Guide (O’Reilly) is the reference I use, and it has served me well.

Get Ajax on Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.