Tomcat is a high-quality, Java-based server. The latest version, Tomcat 4.1, implements Sun Microsystems' Servlet 2.3 and Java ServerPages 1.2 specifications. The code for the original Tomcat was donated by Sun to the Apache project and since then has been completely rewritten.
If you're developing Web applications, or Web services, chances are you'll soon consider using a Tomcat 4.x server. Fortunately, this process is fairly painless if Mac OS X is your platform of choice.
This article walks you through the installation I use to develop and demonstrate Web applications and Web services on my PowerBook G4. I've installed Tomcat so that it loads at startup and runs without having to log in. This setup, along with some port forwarding, means that I can serve live client demonstrations from my laptop at home to clients in other countries.
There are a few flavors of Tomcat, which have been released from a number of sources, so you may end up with a few different installs. I develop Web services as well as traditional Web applications, so I installed the Tomcat that comes with the Java WebServices pack.
|
Related Reading Ant: The Definitive Guide |
The Tomcat that comes with the Java WebServices pack is a simple install that gives me all the bits I need, including a special bunch of Ant tasks that use Tomcat's Manager application to perform temporary installs and removes the .war file without the need to restart. Even though this is an early access release, this version is stable enough for developing and includes the following:
Go to http://java.sun.com/webservices/downloads/webservicespack.html. Then select the Unix install and agree to the terms.
There is something weird about the way Sun sends files to Macintosh users, so the file you download will probably be named Document5. So, after the file has downloaded you must rename it to jwsdp-1_0-ea2-unix.sh, or anything that ends in .sh.
First, make some new folders in your /Library folder. Some people prefer to install things in their local ~/Library folder, but we want our Tomcat to start up before we log in, so /Library is a more appropriate place.
If there isn't already one, make a folder called StartupItems/,
and next to it make one called Tomcat/. Then
copy the jwsdp-1_0-ea2-unix.sh into /Library/Tomcat.
You should be able to do this from the Finder. The folders you create will stay owned by you, and that's fine. You may want to change the those folders' permissions at some stage, but for now just leave them as they are. Some people like to create a special user for Tomcat, but that is overkill.
|
Unix commands you'll learn to love | |
l |
lists everything in that folder |
cd path |
change directory to path |
chmod 777 path |
changes permissions to world read/write and executable. |
ln -s target source |
make soft link to target called source |
Using the terminal cd into /Library/Tomcat and type
chmod 777 jwsdp-1_0-ea2-unix.sh
so that you can execute the installer file. You will probably delete this file later so don't be concerned about the permissions.
./jwsdp-1_0-ea2-unix.sh
This launches the installer application. Click "approve" and "next" a few times. It will ask you if the default java1.3.1 home is OK. It should be fine. If you hit any problems here then Java 1.3.1 is probably stuffed. All Mac OS X installs have Java 1.3 or better.
Try typing java -version to see what version of Java you are running.
Next the installer will ask you for a username and a password. This will be the username and password that Tomcat requests when you administer it using the built-in manager tools. Remember it.
|
The installer will ask you where you wish to install Tomcat. Just type in /Library/Tomcat/jwsdp-1_0-ea2 and click on the next button. The installation will begin. This takes a while, so go get a cuppa tea, and while waiting for the installer to run, do the following:
In /Library/StartupItems make a folder called Tomcat.
|
|
In that folder you will create two files, the first, an executable shell script called Tomcat, and the second a property list called StartupParameters.plist. Both are simple text files.
Tomcat shell script:
#!/bin/sh
##
# Start Tomcat
##
. /etc/rc.common
ConsoleMessage "Starting the Java WebServices framework"
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
export CATALINA_HOME="/Library/Tomcat/Home"
export TOMCAT_HOME="/Library/Tomcat/Home"
export JWSDP_HOME="/Library/Tomcat/Home"
sh ${TOMCAT_HOME}/bin/startup.sh
StartupParameters.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM
"file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>Description</key>
<string>Tomcat Servlet and JSP Server</string>
<key>Messages</key>
<dict>
<key>start</key>
<string>Starting Tomcat Server</string>
<key>stop</key>
<string>Stopping Tomcat Server</string>
</dict>
<key>OrderPreference</key>
<string>None</string>
<key>Provides</key>
<array>
<string>Tomcat</string>
</array>
<key>Requires</key>
<array>
<string>Resolver</string>
</array>
</dict>
</plist>
You can either use a text editor to create the .plist file, or Apple's PropertyList editor.
Make sure the Tomcat script you just added is executable.
chmod 755 Tomcat
|
|
When the Tomcat installer is complete you should see the jwsdp-1_0-ea2 folder in your /Library/Tomcat folder.
Make a soft link to that folder from an alias called Home.
ln -s jwsdp-1_0-ea2 Home
Your TOMCAT_HOME is now always going to be /Library/Tomcat/Home.
If you wish to install other versions of Tomcat, put them in /Library/Tomcat and remake the soft link to Home. This way you don't need to change any other settings.
So give it a go.
cd /Library/StartupItems/Tomcat and type:
./Tomcat to run the Tomcat script.
You should see the following as Tomcat's startup script launches.
Using CATALINA_BASE: /Library/Tomcat/Home
Using CATALINA_HOME: /Library/Tomcat/Home
Using CATALINA_TMPDIR: /Library/Tomcat/Home
Using JAVA_HOME: /System/Library/Frameworks/JavaVM/Versions/1.3.1/Home
Now try to go to http://localhost:8080 using your Web browser.
You should see the Web Services Developer Pack EA2 screen. That's it. You have installed the Web Services Developer Pack. As a final test, restart your Mac. If everything worked properly, you should see "Starting Tomcat Server" appear in your startup window. Let the Mac sit there without logging in.
Now, using a browser on your other Mac (You do have another Mac don't you?) you should be able to go to http://10.0.1.xx:8080 and get the Web services page to open.
Congratulations, you now have a Tomcat installed that boots at startup and runs without requiring a user to log in.
Have a play with the examples and then, when you're feeling up to it, you can start writing your own Web services, servlets, and JSP pages.
Dave Sag is a skilled Object Modeler and Software Designer, using Java as well as other programming environments.
Return to the Mac DevCenter.
Copyright © 2009 O'Reilly Media, Inc.