Home > Articles > Programming > Java

This chapter is from the book

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 Tomcat—now 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 reference—so you can come back to this information as you progress through the book—here'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 path—just 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 path—for 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 it—now 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 window—that'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, congratulations—now you're running your own Web server. Note the URL we've used—http://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 step—getting Tomcat itself running. We've already made progress—now let's see whether we can display some of our own pages using this server.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020