JavaEnterprise JavaServer-Side Development on Mac OS X: Servlets and Tomcat

Server-Side Development on Mac OS X: Servlets and Tomcat

Server-Side Development on Mac OS X

Although Mac OS X is great for client side development,
there is a common misconception that it’s not suited to server-side
development. Just because Mac OS X doesn’t ship with a J2EE implementation,
people seem to think that they can’t do J2EE development.

In this series, we will look at how to use Mac OS X as a killer server-side development environment, or even as a
deployment environment, for those who are so inclined. We’ll start by helping you
to set up Tomcat, and from there we’ll go on to cover other server-side technologies
and how they integrate with OS X.

Whether you plan to develop and deploy on Mac OS X, or to develop locally, and then deploy to another platform, be it Linux, BSD, Solaris, or even NT, OS X (with its
Unix underpinnings, preinstalled Java 1.3.1 runtime, and free professional quality
developer tools) is a rock-solid client-side and server-side development
environment. It also plays well with others, so you can expect your integration with,
and deployment to other platforms to flow smoothly.

Servlets and Tomcat

In this article, we will install Tomcat, the standard
reference platform from Apache and Sun — although there are plenty of other
Java-based servlet environments that should run fine too. In this article we’ll get
Tomcat installed, and next week we’ll see how to launch it automatically and securely
on startup. We’ll then look at how to make it work with Apache, and finally we’ll get
set up do to some servlet and JSP development in Project Builder, the professional-quality development environment you get for free with each copy of OS X.

If you’ve installed or used Tomcat before, most of this will
be the same for you. As has been so often pointed out, OS X is a Unix-based
operating system, so installation here is almost identical to installation on
any other Unix flavor. As with other flavors, though, there are some choices to
make about where to install the files, and in where the files live that control starting and stopping the server.

We’ll also look at how to make your installation secure. We
don’t necessarily want to run lots of new code as root, after all, and when
you’re running a servlet, it runs of course with the permissions of the server
process inside of which it’s running. This isn’t any different from other
platforms, although again, some of the configuration specifics are a little
different.

So enough about what we’re going to cover, let’s dive in,
and set up Tomcat.

As of this writing, the current version of Tomcat is 4.0.1,
and that is what we’ll be installing. Note, however, that the installation
steps are similar or identical with other versions, so if you choose to install
3.3, or if Tomcat 4.5 has just shipped as you’re reading this, you should find
the installation process very similar.

This article assumes that you logged in as a user with
administrator privileges on the machine you’re developing on. If not, you can
either get your machine administrator to follow these instructions, or you can
install Tomcat in your home directory, and start and stop it manually by
running <tomcat-home>/bin/startup.sh and
<tomcat-home>/bin/shutdown.sh from a terminal window.

Let’s start by downloading the Tomcat binary. The one we
want is http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.1/bin/jakarta-tomcat-4.0.1.tar.gz. You’re welcome to build your own copy from source, and it’s easy enough to do. Just follow the Unix instructions on the Jakarta.apache.org website. But of
course, since Tomcat is written in Java, there’s really no need.

NOTE: By default,
your Web browser will typically use Aladdin StuffIt Expander to un-gzip and
then un-tar the file once it’s been downloaded. Unfortunately, StuffIt Expander
seems to like to truncate file names at 31 characters, which isn’t so good when
one of the files in the distribution is named SetCharacterEncodingFilter.class. This un-tars as SetCharacterEncodingFilter.clas, making your
servlets and jsp.s not work.

Not to worry, we’ll just untar the file with the
command-line gnutar utility instead. You can just throw away the
jakarta-tomcat-4.0.1.tar file and the jakarta-tomcat-4.0.1 folder StuffIt
Expander creates.

The next step is to decide where to install Tomcat. I like
to create a symbolic link to my Tomcat install along side the pre-installed Apache directory (/Library/WebServer), and to install third-party binaries under /user/local, as is the standard on many Unix distributions, OS X included. Then, when Tomcat 5 comes out, you can install it in /usr/local/jakarta-tomcat-5.0.0, update your symlink, and move over your Web apps, and your transition should go smoothly. This is also important if you need to have multiple versions of Tomcat installed and running concurrently, which is often the case during development and testing. Of course, how you arrange your files is entirely up to you. If you install them somewhere else, just remember as we go along to change the instructions accordingly.

Open your Terminal application. (It’s found in the Utilities
folder inside of the Applications folder. You’ll be using this a lot as a Java
developer, so you may want to drag it into the dock.)

Now un-tar the Tomcat distribution, and copy the resulting
files into the newly created directory:

(Assuming that the distribution was downloaded to your
desktop:)

cd /usr/local
sudo gnutar -xzvf ~/Desktop/jakarta-tomcat-4.0.1.tar.gz
Password: <your password>
sudo ln -s /usr/local/jakarta-tomcat-4.0.1 /Library/Tomcat

Note: The first time you use sudo, you will be prompted for your password. Enter the same password you use to log into Mac OS X. sudo is a helpful Unix security utility that lets you explicitly
request root privileges for a single command, while being logged in as a regular user
who has administrator privileges.

You now have a complete install of Tomcat. If you’d like,
you can run it now by typing

sudo /Library/Tomcat/bin/startup.sh

You should now be able to connect to Tomcat from your Web
browser by going to http://localhost:8080/
and all the example apps should work.

You can start and stop Tomcat by running:

sudo /Library/Tomcat/bin/startup.sh

…and…

sudo /Library/Tomcat/bin/shutdown.sh

…respectively.

Now that we have Tomcat installed and running, next time we’ll look at how to set up OS X to launch Tomcat automatically on startup and how to run it more securely, by running it with the permissions of an unprivileged user.

About the Author

Ian McFarland is the president of Neo Ventures Ltd., a software consulting company in San Francisco. Previously, he served as senior director of technology for Hollywood Stock Exchange and Java Evangelist for Symantec Internet Tools.

Latest Posts

Related Stories