| [ directory ] |
Servlets and JavaServer Pages, also meaning all containers, rely on the Java programming language. In the case of Servlets, the code is nothing more than a Java class that implements the appropriate interface(s). Compiling a Servlet is identical to compiling a Java class. JSP is slightly more abstract. JSP, is compiled and used exactly like a Servlet, but the process is almost always done automatically, without a JSP developer noticing. When authoring a JSP, a developer uses a combination of markup and code to make a page that usually resembles an HTML or XML document. This page is compiled into a class file by the Servlet container and automatically loaded.
The official Java platform is designed, owned, and managed by Sun Microsystems. Unlike most other programming languages, Java is not designed for you to compile to platform-specific instructions. Instead Java is compiled down to byte code that gets interpreted by the computer running your Java program. This means a Java program developed and compiled on one computer will run on any other computer with Java support; Servlets and JSP can be compiled and run on most any operating system. This includes most all the Windows, Linux, and Macintosh operating systems. This book assumes you are installing a JSP environment on one of these systems and instructions are provided for installation on each.
Java 2 Standard Edition 1.4 (J2SE 1.4) support is required for code examples in this book. Sun Microsystems provides a free reference implementation of Java 1.4 online. Unless you are using Macintosh OS X, go to http://java.sun.com/j2se/1.4/ and download the latest Java distribution for your computer. Macintosh OS X has proprietary support for Java 2. If you are running OS X, no additional downloads are needed.
Java only needs to be installed once on your computer and only for the specific operating system you are using. It is not required that you read all of the sections covering installation on all of the featured platforms. They exist only to give readers a guide to their specific operating system. Complete coverage of the Java 2 Standard Edition 1.4 is outside the scope of this book; however, later use of the J2SDK by this book will not require comprehensive knowledge of the tools provided by Sun. If you are looking for a detailed guide for this information, refer to Thinking In Java, 3rd Edition[2], by Bruce Eckel.
[2] A free copy of the book can be found online, http://www.mindview.net/Books/TIJ/.
Microsoft Windows comes in many varieties, with early versions having a big distinction between a desktop computer and a computer designed to be a server. Practically speaking, a desktop computer running Windows 95 or Windows 98 is a not a good choice for installing a production environment for Servlets and JSP, but a Windows 95 or Windows 98 computer will suffice in order to try this book's examples. The Java distribution for Microsoft Windows will run on all versions of the operating system excluding Windows 3.x. In this book, the focus is on installing Java on a Windows NT, 2000, or XP computer. However, we realize that many readers have a desktop PC running Windows 95 or Windows 98 at home. An attempt will be made to help you if this is the case.
After downloading the J2SE 1.4, it must be installed on your system. The download should be an executable file that can be run by double-clicking it. Double-click on this file and follow the installation wizard through all the steps. It does not matter where you install the J2SE 1.4, but it is worth noting the location, as it is needed in a later part of this chapter.
Installation of the Java 2 Standard Development Kit 1.4 is now complete. Skip ahead to the section entitled, "Tomcat".
Linux comes in far more varieties than Windows and operates on many more hardware architectures. A walk-through installation guide for all Linux distributions is not attempted, but this guide should work on the vast majority of distributions. Specifically this section gives a walk-through of installing the J2SE 1.4 on Red Hat Linux 7.3. It will greatly resemble installation on any Linux distribution for x86 processors, as an RPM is not used. If you downloaded the RPM or equivalent for your distribution, feel free to install it, make note of the installation directory, and skip to the next section of this chapter.
At the introduction to this section, you should have downloaded the J2SE 1.4 Linux binary installation file. The file should be named something similar to j2sdk-1_4_0_01-linux-i586.bin with appropriate version numbers for the latest release. Any post-1.4 release should be adequate; this guide uses version 1.4.0_01. From the command prompt make sure the file has executable permissions and execute the program. These are what the commands would be to make the file executable and then execute it; assume the download is in the /root/download directory and you have proper permissions.
chmod +x /root/download/j2sdk-1_4_0_01-linux-i586.bin /root/download/j2sdk-1_4_0_01-linux-i586.bin
When the file executes, Sun's licensing terms are displayed and you have the option of agreeing to continue the installation. Figure 1-5 shows an example display of the licensing terms.

If you agree to the terms, files will automatically be unpacked to a j2sdk1.4.0 directory created by the installation program. You can move this directory to any location you prefer, but remember the location where the J2SDK 1.4 is because it will be needed later when the environment variables are set. Installation of the standard Java development kit is now complete.
It is possible you are either an advanced user who dislikes non-optimized code or that you do not have an x86 microprocessor. Both of these cases are largely outside the scope of this book, but a quick pointer should get you well on your way if that is what you seek. The "official" Java Linux code is maintained at Blackdown Linux, http://www.blackdown.org. Visit Blackdown Linux if you need the source code to either optimize your Java distribution or compile it for a different architecture.
Tomcat, the reference implementation of Servlets and JSP, is part of the open source Apache Jakarta project and is freely available for download from http://jakarta.apache.org/tomcat[3]. Tomcat can be run on Windows, Linux, Macintosh, Solaris, and most any of the other Unix distributions. You can use Tomcat both commercially and non-commercially as specified by the Apache Software Foundation License.
[3] This part of the book is perhaps the most important. If for any reason you are having trouble installing Tomcat, please consult the book support site, http://www.jspbook.com, for a complete, up-to-date Tomcat 5 installation guide.
The next few sections provide a walk-through for installing Tomcat on Windows, Linux distributions, and Macintosh OS X. If needed, follow the appropriate section to get Tomcat correctly set up for later examples.
The Tomcat installation for Windows greatly resembles installing any other piece of Windows software. The process involves downloading the Tomcat installation executable file and running it to launch the Windows installation wizard. After a few simple clicks, the wizard will have set up Tomcat to work with your Windows distribution.
If you have not done so already, download the Windows executable file for the Tomcat installation. You can find it at the following URL, http://jakarta.apache.org/builds/jakarta-tomcat/release/. Simply follow the links to the latest Tomcat 5 Windows executable fileas of this writing, the 5.0.2 beta release of Tomcat is at http://jakarta.apache.org/builds/jakarta-tomcat/release/v5.0.2-alpha/bin/jakarta-tomcat-5.0.2.exe. After downloading the executable, double-click on it to start the installation wizard. A screen similar to Figure 1-6 should appear notifying you that the Tomcat Installer located a J2SDK installation.

Click OK and continue on to the screen displaying the Apache Software Foundation license. Read through the terms and make sure you agree with Tomcat's licensing. If you accept the terms, the installation continues and you can choose what components you wish to install. The default ones should be fine, as shown in Figure 1-7.

Proceed with the installation wizard to the next screen, and you can choose a directory to install Tomcat. Figure 1-8 shows the corresponding installation wizard screen. Choose any directory you prefer.

After choosing an installation directory, the wizard will ask for some initial Tomcat configuration information, including a port number and administrative access information. The default options are fine, but later on in the chapter we will be changing the port Tomcat uses to be port 80 instead of 8080. You may change the port to 80 via the installation wizard now or later on in the chapter. Figure 1-9 displays the corresponding installation wizard screen.

The final piece of configuration information Tomcat requires is the location of your Java Virtual Machine, which should be wherever you installed it earlier in the chapter. By default Tomcat attempts to locate the most recent JVM for you, as shown in Figure 1-10.

The default JVM found by the installation wizard should be fine, but you can change the location to be any JVM you desire (note a Java 1.4-compatible JVM is required to execute this book's examples). Finally, the installation wizard will automatically install all the Tomcat files. Figure 1-11 shows the corresponding installation wizard screen.

To complete the installation and run Tomcat, start the server by double-clicking on the Tomcat icon or by executing the startup script startup.bat found in the TOMCAT_HOME/bin directory. Check the service is running by browsing to your local computer on port 8080 (or 80 if you changed Tomcat's port via the installation wizard), http://127.0.0.1:8080. A Tomcat welcome page should be displayed[4] as shown in Figure 1-12.
[4] Port 8080 should not be previously in use, but if it is, you will not be able to see the Tomcat welcome page. If you are sure you have correctly installed Tomcat and think a port bind is causing problems, consult the later section in this chapter which deals with changing the port Tomcat runs on.

Installation of Tomcat is complete, and you are now ready to run book examples. Tomcat comes bundled with documentation and examples of both Servlets and JSP. Feel free to examine the bundled examples, but this book will not cover them further. Before continuing, there are two important scripts to be aware of.
startup.bat: The startup.bat script in the TOMCAT_HOME/bin directory is used to start the Tomcat container and Web server. Servlet and JSP code examples rely on Tomcat, and you must have Tomcat turned on before testing them.
shutdown.bat: The shutdown.bat script in the TOMCAT_HOME/bin directory is used to terminate the Tomcat container and Web server.
It is important to be aware of these two scripts because from time to time an example will require that Tomcat be restarted. When this is asked, it is assumed Tomcat is currently running and implies that the shutdown.bat script and then startup.bat be executed to reload the server. Alternatively you can also use an automated utility like Ant, which is discussed later in this chapter, to manage reloading, compiling, and other repetitive aspects of developing Servlets and JSP.
Just because you are using Windows 9x, or Millennium, does not mean you will have a problem, but be aware that a common problem does exist. If when executing startup.bat or shutdown.bat, an "out of environment space" error occurs, do the following. Right-click on the startup.bat and shutdown.bat files. Click on Properties then on the Memory tab. For the Initial environment field, enter 4096. After you click apply, Windows will create shortcuts in the directory which you can use to start and stop the container. Use these shortcuts to start and stop Tomcat.
What is happening is that the batch file is not allocating enough memory to execute its commands. The fix is to simply add more memory. The Tomcat developers are aware of this problem and are trying to incorporate fixes into startup.bat and shutdown.bat.
The Tomcat project is continuously expanding the different types of installation packages in which Tomcat releases are available. If you notice a Tomcat installation package that matches special installation software for your specific Linux distribution, feel free to download and use the appropriate file. For this walk-through, installation will be of the compiled Java code packaged in a tarball[5]. Download the Tomcat tarball jakarta-tomcat-5.0.tar.gz" from http://jakarta.apache.org/builds/jakarta-tomcat/release/. Installation is as easy as decompressing the Tomcat binaries and setting two environment variables.
[5] A tarball is a commonly used Unix term for a file compressed using gzip and tar compression. The de facto standard for distributing Unix and Linux programs is by means of a tarball because the compression is often significantly better than only ZIP.
Decompress the tarball by using the tar and gzip compression utilities from the command prompt. Here is what the command would look like if you had placed the download in the /usr directory.
gunzip -c /usr/jakarta-tomcat-5.0.tar.gz | tar xvf -
The Tomcat binaries will be available in the /usr/jakarta-tomcat-5.0 directory or a similarly named directory that matches the version of Tomcat you downloaded. For the rest of this walk-through, it is assumed this is the directory you are also using. If not, change the directory name in examples to match the location where you uncompressed Tomcat.
Before starting Tomcat, two environment variables need to be set: JAVA_HOME and TOMCAT_HOME. The JAVA_HOME variable corresponds to the directory of the J2SDK 1.4 installed earlier in this chapter. The TOMCAT_HOME variable corresponds to the directory into which you just uncompressed Tomcat. Set and export the two environment variables. The commands would be similar to the following if the J2SDK is installed in the /usr/java/jdk1.4 directory and Tomcat 5 is installed in the /usr/jakarta-tomcat-5.0 directory; replace each variable accordingly to match your specific case:
JAVA_HOME=/usr/java/jdk1.4 TOMCAT_HOME=/usr/jakarta-tomcat-5.0 export JAVA_HOME TOMCAT_HOME
Tomcat is now ready to run. Start the server by executing the startup script startup.sh found in the /usr/jakarta-tomcat-5.0/bin directory. Check the service is running by browsing to your local computer on port 8080, http://127.0.0.1:8080. A Tomcat welcome page should be displayed[6] as shown in Figure 1-13.
[6] Port 8080 should not be previously in use, but if it is, you will not be able to see the Tomcat welcome page. If you are sure you have correctly installed Tomcat and think a port bind is causing problems, consult the later section in this chapter, which deals with changing the port Tomcat runs on.

Installation of Tomcat is complete, and you are now ready to run book examples. Tomcat comes bundled with documentation and examples of both Servlets and JSP. Feel free to examine the bundled examples, but this book will not cover them further. Before continuing on, there are two important scripts to be aware of.
startup.sh: The startup.sh script in the TOMCAT_HOME/bin directory is used to start the Tomcat container and Web server. Servlet and JSP code examples rely on Tomcat, and you must have Tomcat turned on before testing them.
shutdown.sh: The shutdown.sh script in the TOMCAT_HOME/bin directory is used to terminate the Tomcat container and Web server.
It is important to be aware of these two scripts because from time to time an example will require that Tomcat be restarted. When this is asked, it is assumed Tomcat is currently running and implies that the shutdown.sh script and then startup.sh be executed to reload the server. Also be aware that the environment variables previously set will not persist between different terminal sessions and need to either be included in a startup script or set for each session.
Alternatively, you can also automate the redundant process of compiling book examples and reloading Tomcat by using a build utility. Use of the Jakarta Ant build utility is strongly encouraged with this book and is covered later in this chapter.
Tomcat is a robust JSP and Servlet container. It not only provides complete support for the JSP and Servlet specifications, but it can also act as a standalone Web server. By default, this is exactly what Tomcat does, and the default configuration is all we need for this book's examples. Full instructions on configuring Tomcat are outside the scope of this book. There are many different Servlet and JSP containers available, and it is not very practical to devote a large part of this book to Tomcat-specific information. There is only one important aspect of configuring Tomcat that needs to be discussed. If you would like to learn more about using Tomcat to its full potential, some good resources are listed at the end of this section.
For all practical purposes it does not matter what port you run Tomcat on. Ports 8080, 80, 1234, and 9999 all work the same. However, any port besides 80, which is being used for HTTP, comes with some slight annoyances that it would be nice to avoid. Specifically, the default port for HTTP is port 80. Recall the previously used URL for Tomcat's welcome page, http://127.0.0.1:8080/index.html. The localhost address, 127.0.0.1, is universal, but the 8080 is required because Tomcat's HTTP connector is not listening on port 80; the default configuration for Tomcat is for it to listen on port 8080. The 8080 is slightly annoying, especially when needing to add the 8080 to all local absolute links in your Web application.
In order to simplify the book examples and to avoid confusion, we will now configure Tomcat to use port 80, which is the default HTTP port. This is done by editing Tomcat's configuration file, /conf/server.xml. Open this file with your favorite text editor and do a search for '8080'; you should find the following entry:
<!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
<Connector className="org.apache.coyote.tomcat5.CoyoteConnector"
port="8080" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0"
connectionTimeout="20000"
useURIValidationHack="false" />
The entry is responsible for configuring Tomcat's HTTP connector and the port it listens on. Change the entry to use port 80 by replacing 8080 with 80:
<!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
<Connector className="org.apache.coyote.tomcat5.CoyoteConnector"
port="80" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0"
connectionTimeout="20000"
useURIValidationHack="false" />
Tomcat will then listen for HTTP requests on port 80, which is assumed when no port is specified. Shut down and restart Tomcat so that it uses the new port. Both http://127.0.0.1 and http://127.0.01:80 will now display the Tomcat welcome page. Likewise, all subsequent requests, which do not specify a port, will be directed to Tomcat. Make sure to restart Tomcat before testing out the changes[7].
[7] If another service, such as Apache or IIS, is running on port 80, Tomcat will not be able to use the port. Either choose a different port, configure Tomcat to work with the service, terminate the conflicting service, or change the services default port. Additional help with configuring Tomcat to use an existing Web server is outside the scope of this book.
The Tomcat's User's Guide is the official documentation for Tomcat. This should be the first place you look for help when configuring and using Tomcat. The Tomcat User's Guide can be found online at the Jakarta Tomcat Web site, http://jakarta.apache.org/tomcat.
The Tomcat user mailing list is the best place to find community support for configuring and using Tomcat. This mailing list consists of most all the Tomcat developers and many of the current users of Tomcat. By posting a question on this mailing list, you can try to solicit information from the current Tomcat experts. You can subscribe to this mailing list by following the instructions on the Jakarta Mailing List page, http://jakarta.apache.org/site/mail.html.
Be warned the Tomcat user mailing list generates a lot of traffic. A hundred or more emails a day is not uncommon; however, it is only an issue if you do not wish to deal with that volume of email. There are many benevolent people, which is obvious on the Tomcat user mailing list because of the unbelievable amount of questions they answer. An alternative to using the Tomcat user mailing list is to consult the online archives, which can be found using the same link.
Book Support SiteFor the most current information and updates of this chapter's walk-through guides, use the book support site, http://www.jspbook.com. Along with the latest versions of the walk-through guides, this site also contains a Frequently Asked Question (FAQ) section for questions relating to this book. Many of these questions answer problems relating to Tomcat configuration issues for various software environments. Additionally, the book support site is intended to provide a place that can deal with any unexpected issues occurring after publication. |
| [ directory ] |