The Tomcat Server
Now that you've installed Java, you're halfway there. The next step in creating your JSP development environment is to install a JSP-enabled Web server on your computer. There's nothing complex in this, although the idea of installing a Web server on your computer can sound a little daunting.
There are a number of JSP-enabled Web servers, and we'll use the one that's become the JSP standard: the Apache project's Tomcat server. This server is the standard throughout the JSP world, and it's the one Sun uses when creating its JSP implementation.
You can download the Tomcat server at http://jakarta.apache.org/tomcat/ ("Jakarta" is the name of the project that Tomcat is a part of). We'll use the most recent Tomcat version available as of this writing, version 4.0.3, which supports JSP version 1.2. Currently, you can download this version from http://jakarta.apache.org/tbuilds/jakarta-tomcat-4.0/release/v4.0.3/bin/. Just pick the appropriate version for your system, such as jakarta-tomcat-4.0.3.zip for Windows, and unzip it in a directory of your choosing.
NOTE
In this book, the Zip file will be copied to the Windows directory C:\tomcat and unzipped there, which creates the directory C:\tomcat\jakarta-tomcat-4.0.3, and plenty of subdirectories underneath it. You might want to use the same directory structure to make it easier to follow the examples we'll develop (substitute / for \ in Unix).
That installs Tomcatnow let's take a look at what we've got.
The Tomcat Directory Structure
A great deal of this book has to do with understanding how the standard JSP server, Tomcat, works. For the sake of referenceso you can come back to this information as you progress through the bookhere's the directory structure that unzipping Tomcat creates (the "classes" here refer to Java classes; for Java code to be accessible, it must be in a class, and you're going to see more about classes in Day 6, "Creating JSP Components: JavaBeans"):
jakarta-tomcat-4.0.3 |__bin Binary executable files |__classes Classes available to all Web applications |__common Classes available to internal classes and Web applications | |__classes Common Java classes | |__lib Common Java classes in Java Archive (JAR) format |__conf Configuration files (such as passwords) |__jasper JAR files used to process and run JSP pages |__lib JAR files available to Web applications |__logs The server's log files |__server Internal Tomcat classes |__webapps Directory for Web applications |__work Scratch directory for holding temporary files
We'll get more familiar with this directory structure when we start using it in depth in the coming days.
Note in particular the webapps directory, which is where the Web applications you create go so they're accessible to the browser. For instance, the examples from Day 1 will go into the ch01 directory we'll add to webapps:
webapps |__ch01 Our folder for Day 1 examples
The directory containing Web applications must also contain a directory named WEB-INF, with two subdirectories, classes and lib. WEB-INF, classes, and lib might all be empty, but Tomcat will expect them to be there before running your application:
webapps |__ch01 Our folder for Day 1 examples |__WEB-INF Information about Day 1's Web applications |__classes Java classes used by Day 1's Web applications |__lib JAR files used by Day 1's Web applications
We'll create the ch01, WEB-INF, classes, and lib directories we need in a moment, but first, let's start Tomcat itself.
NOTE
There will be plenty of path specifications in this book, such as jakarta-tomcat-4.0.3\webapps\ch01 in Windows, or jakarta-tomcat-4.0.3/webapps/ch01 in Unix. If you're using Windows and you see forward slashes (/) in a path, it's a Unix pathjust substitute backslashes (\) instead, and vice versa if you're a Unix user who sees forward slashes. The Tomcat directory structure is the same on all operating systems, except for operating system-dependent syntax, like using \ instead of /.
Starting Tomcat
When you start Tomcat, you must first set a few environment variables. Among other things, these environment variables will let Tomcat find Java so it can run our JSP code.
TIP
You can get the details on these environment variables by taking a look at the readme.txt document that comes with Tomcat, which will refer you to the document running.txt. It's also available online at http://jakarta.apache.org/tomcat/tomcat-4.0-doc/RUNNING.txt.
In particular, the environment variables to set are the following:
JAVA_HOME points to the installation directory of Java; for example, that might be C:\jdk1.4 in Windows.
CATALINA_HOME points to the installation directory of Tomcat; for example, C:\tomcat\jakarta-tomcat-4.0.3 (you get this path when you unzip Tomcat 4.0.3 in C:\tomcat).
PATH holds the search path the computer's operating system will search for programs to run. Make sure you add Java's bin directory to your pathfor example, you would add C:\jdk1.4\bin to the path if Java was installed to C:\jdk1.4.
The way you actually set these variables varies by operating system. For example, to set JAVA_HOME to C:\jdk1.4 in Windows 2000 Professional, you can select Start, Settings, Control Panel to open the control panel, and double-click the System icon in the control panel. Next, click the Advanced tab as you see in Figure 1.3. Doing so opens the Environment Variables dialog box; click the New button in the System Variables part, opening the New System Variable dialog you see in Figure 1.4.
Figure 1.3 The Advanced tab in the System Properties dialog in Windows 2000 Professional.
Figure 1.4 Setting an environment variable in Windows 2000 Professional.
You can enter the new setting for JAVA_HOME as you see in Figure 1.4: C:\jdk1.4. To change the PATH variable, which already exists, you click the Edit button in the System Variables section and edit the PATH variable to add the Java bin directory to it. For example, if your path looks something like this:
C:\WINDOWS;C:\Program Files
you'd add a semicolon and the path of the Java bin directory:
C:\WINDOWS;C:\Program Files;C:\jdk1.4\bin
How you set environment variables depends on the operating system, however; for example, the process is completely different in Windows 95/98, where you must edit the C:\autoexec.bat file instead.
TIP
You can find excellent instructions on setting environment variables for all the operating systems that run Java from the Java download page, in the installation notes. Here's the URL with links for various operating systems: http://java.sun.com/j2se/1.4/install-operating system name.html (for example, for Windows, the URL is http://java.sun.com/j2se/1.4/install-windows.html). These notes are all about setting the PATH environment variable, but you can use them to set any environment variable.
You can also set the environment variables when you start Tomcat itself, and you might find that more convenient. You'll find the directions for starting Tomcat in the running.txt document that comes with Tomcat.
To start Tomcat, you'll need a command prompt. In Windows, that's the DOS prompt, which you get in a DOS window. You can open a DOS window by selecting Start, Programs, Accessories, Command Prompt in Windows 2000 Professional; Start, Programs, MS-DOS Prompt in Windows 98, and so on.
To set the environment variables (if you haven't set them already), you can type the following at the DOS prompt. (Make the version numbers and paths match what you have installed, of course. )
C:\>SET JAVA_HOME=C:\jdk1.4 C:\>SET CATALINA_HOME=C:\tomcat\jakarta-tomcat-4.0.3 C:\>SET PATH=%PATH%;C:\jdk1.4\bin
Note the last statement, SET PATH=%PATH%;C:\jdk1.4\bin, which is an easy way of adding C:\jdk1.4\bin to the end of the path, without making any changes to the rest of the path.
In the Unix bash shell, this might look something like this in your .bashrc file (start a new shell to make the changes take effect):
JAVA_HOME=/jdk1.4 export JAVA_HOME SET CATALINA_HOME=/tomcat/jakarta-tomcat-4.0.3 export CATALINA_HOME SET PATH=/usr/local/bin:/jdk1.4/bin export PATH
In the Unix tcsh shell, it might look like this in your .tcshrc file (start a new shell to make the changes take effect):
setenv JAVA_HOME /jdk1.4 setenv CATALINA_HOME /tomcat/jakarta-tomcat-4.0.3 setenv PATH /usr/local/bin:/jdk1.4/bin
After these environment variables are set, we're ready to start Tomcat. Again, this process is operating system-dependent; see the Tomcat document running.txt for more details. In Windows, go to the Tomcat bin directory (the bin directory is right under the directory that Tomcat unzips itself to, such as C:\tomcat\jakarta-tomcat-4.0.3\bin if Tomcat was unzipped in C:\tomcat) and type startup:
C:\tomcat\jakarta-tomcat-4.0.3\bin\>startup
In Unix, that command might look like this:
/tomcat/jakarta-tomcat-4.0.3/bin/startup.sh
That's itnow Tomcat is running. In Windows, you'll see a new DOS window open and a message similar to this appear:
Starting service Tomcat-Apache Apache Tomcat/4.0.3
Don't close this new windowthat's where Tomcat is running. So how do we put Tomcat to work? You'll need a browser, such as Microsoft Internet Explorer (which you can pick up free at http://www.microsoft.com/windows/ie/default.asp), or Netscape Navigator (free at http://browsers.netscape.com/browsers/main.tmpl). Run the browser now and enter the URL http://localhost:8080/index.html, which should bring up the page you see in Figure 1.5.
Figure 1.5 Running the Tomcat server.
As the figure says, congratulationsnow you're running your own Web server. Note the URL we've usedhttp://localhost:8080/index.html. The localhost part is the name of the Web server, and localhost is the name reserved for Web servers running on your own machine. 8080 is the port number. Each Web server uses a "port" number to keep it separate from other Web servers. Usually, Web servers use port 80, but Tomcat uses port 8080 so it won't interfere with other Web servers.
TIP
If Tomcat doesn't run for you, see the Troubleshooting section in the running.txt document that comes with Tomcat.
To stop Tomcat, you use the shutdown command. It looks something like this in Windows:
C:\tomcat\jakarta-tomcat-4.0.3\bin\>shutdown
and this in Unix:
/tomcat/jakarta-tomcat-4.0.3/bin/shutdown.sh
That's our first stepgetting Tomcat itself running. We've already made progressnow let's see whether we can display some of our own pages using this server.