Академический Документы
Профессиональный Документы
Культура Документы
Tomcat
A Tutorial on Installing and
Using
Jakarta Tomcat 5.5 for
Servlet and JSP Development
Executive Summary
Most people will just want to download the preconfigured Tomcat
version, set JAVA_HOME and CLASSPATH, and they are done. But
following is a summary of the steps for people who want to change
the configuration or do it themselves. This section gives a summary of
the required steps; I give extremely detailed instructions in the
following sections.
Install the JDK. Make sure JDK 1.5 or 1.4 is installed and your PATH is set so
that both "java -version" and "javac -help" give a result.
Configure Tomcat.
1. Download the software. Go to http://tomcat.apache.org/download-55.cgi
and download and unpack the zip file for the current release build of
Tomcat 5.5. Using Tomcat with JDK 1.5 (Java 5) is preferred, but if you
use JDK 1.4, you have to also download and unpack the "Compat" zip
file. The the preconfigured Tomcat version already contains the
"Compat" files and can be used with either Java 5 (1.5) or Java 1.4.
2. Set the JAVA_HOME variable. Set it to refer to the base JDK directory, not
the bin subdirectory.
3. Change the port to 80. Edit install_dir/conf/server.xml and change the
port attribute of the Connector element from 8080 to 80.
4. Turn on servlet reloading. Edit install_dir/conf/context.xml and change
<Context> to <Context reloadable="true">.
5. Enable the invoker servlet. Go to install_dir/conf/web.xml and
uncomment the servlet and servlet-mapping elements that map the
invoker servlet to /servlet/*.
6. Change the DOS memory settings. If you are on Windows 98/Me and get
an "Out of Environment Space" error message when you start the server,
right-click on install_dir/bin/startup.bat, select Properties, select
Memory, and change the Initial Environment entry from Auto to at least
2816. Repeat the process for install_dir/bin/shutdown.bat. Only needed
for Windows 98 and ME; not necessary on recent versions of Windows.
7. Set the CATALINA_HOME variable. Optionally, set CATALINA_HOME to
refer to the top-level Tomcat installation directory. Not necessary unless
you copy the startup scripts instead of making shortcuts to them.
8. Use a preconfigured Tomcat version. Optionally, use a version of Jakarta
Tomcat that has all of the above changes already made, and has the test
HTML, JSP, and servlet files already bundled. Just unzip the file, set
your JAVA_HOME and CLASSPATH variables, and you are ready to go.
Test the server.
1. Verify that you can start the server. Double-click
install_dir/bin/startup.bat and try accessing http://localhost/.
2. Check that you can access your own HTML & JSP pages. Drop some
simple HTML and JSP pages into install_dir/webapps/ROOT and access
them with http://localhost/filename.
3. Try Compiling and Deploying Servlets.
Set up your development environment.
1. Create a development directory. Put it anywhere except within the
Tomcat installation hierarchy.
2. Make shortcuts to the Tomcat startup & shutdown Scripts. Put shortcuts
to install_dir/bin/startup.bat and install_dir/bin/shutdown.bat in your
development directory and/or on your desktop.
3. Set your CLASSPATH. Include the current directory ("."), the servlet/JSP
JAR files (install_dir/common/lib/servlet-api.jar and
install_dir/common/lib/jsp-api.jar), and your main development
directory from Step 1.
4. Bookmark the servlet & JSP javadocs. Add install_dir/webapps/tomcat-
docs/servletapi/index.html and install_dir/webapps/tomcat-
docs/jspapi/index.html to your bookmarks/favorites list.
Compile and test some simple servlets.
1. Test a packageless servlet. Compile a simple servlet, put the .class file in
install_dir/webapps/ROOT/WEB-INF/classes, and access it with
http://localhost/servlet/ServletName.
2. Test a servlet that uses packages. Compile the servlet, put the .class file
in install_dir/webapps/ROOT/WEB-INF/classes/packageName, and
access it with http://localhost/servlet/packageName.ServletName.
3. Test a servlet that uses packages and utility classes. Compile a servlet,
put both the servlet .class file and the utility file .class file in
install_dir/webapps/ROOT/WEB-INF/classes/packageName, and access
the servlet with http://localhost/servlet/packageName.ServletName. This
third step verifies that the CLASSPATH includes the top level of your
development directory.
Establish a simplified deployment method.
1. Copy to a shortcut. Make a shortcut to install_dir/webapps/ROOT. Copy
packageless .class files directly there. With packages, copy the entire
directory there.
2. Use the -d option of javac. Use -d to tell Java where the deployment
directory is.
3. Let your IDE take care of deployment. Tell your IDE where the
deployment directory is and let it copy the necessary files.
4. Use ant or a similar tool. Use the Apache make-like tool to automate
copying of files.
Get more info. Access the complete set of Tomcat docs, get free JSP and servlet
tutorials, read the official servlet and JSP specifications, get JSP-savvy editors
and IDEs, look for J2EE jobs, etc.
Install the JDK
Your first step is to download and install Java. The servlet 2.4 (JSP 2.0)
specification requires JDK 1.3 or later; J2EE 1.4 (which includes
servlets 2.4 and JSP 2.0) requires JDK 1.4 or later. You might as well
get a recent Java version, so use JDK 1.4 or 1.5. If you know which of
those Java versions will be used on your project, get that one. In
general, I recommend JDK 1.5. See the following sites for download
and installation information.
JDK 1.5 for Windows, Linux, and Solaris:
http://java.sun.com/j2se/1.5.0/download.jsp. Be sure you download the full JDK
(J2SE Development Kit), not just the JRE (Java Runtime Environment). The
JRE is only for running already-compiled .class files, and lacks a compiler.
JDK 1.4 for Windows, Linux, and Solaris:
http://java.sun.com/j2se/1.4/download.html. Be sure you download the full SDK
(Software Development Kit), not just the JRE (Java Runtime Environment). The
JRE is only for running already-compiled .class files, and lacks a compiler.
JDK 1.5 and 1.4 for MacOS: http://developer.apple.com/java/download/.
On Windows XP, you could also go to the Start menu, select Control
Panel, choose System, click on the Advanced tab, press the
Environment Variables button at the bottom, and enter the PATH
variable and value directly. On Windows 2000 and NT, you do Start,
Settings, Control Panel, System, then Environment. However, you can
use C:\autoexec.bat on those versions of Windows also (unless a
system administrator has set your PC to ignore it).
Configure Tomcat
Configuring Tomcat involves five main steps and four optional steps:
1. Downloading the Jakarta Tomcat software.
2. Setting the JAVA_HOME variable.
3. Changing the port from 8080 to 80.
4. Telling Tomcat to reload servlets when they are modified.
5. Enabling the invoker servlet
6. Changing the DOS memory settings. (Win98/Me only)
7. Setting the CATALINA_HOME variable. (Optional; rarely used)
8. Using a preconfigured version of Tomcat with these changes already made.
(Optional; widely used)
9. Using the Windows .exe installer instead of the .zip file. (Not Recommended)
On Windows XP, you could also go to the Start menu, select Control
Panel, choose System, click on the Advanced tab, press the
Environment Variables button at the bottom, and enter the JAVA_HOME
variable and value directly. On Windows 2000 and NT, you do Start,
Settings, Control Panel, System, then Environment. However, you can
use C:\autoexec.bat on those versions of Windows also (unless a
system administrator has set your PC to ignore it).
3. Change the Port to 80
Assuming you have no other server already running on port 80, you'll
find it convenient to configure Tomcat to run on the default HTTP port
(80) instead of the out-of-the-box port of 8080. Making this change
lets you use URLs of the form http://localhost/blah instead of
http://localhost:8080/blah. Note that you need admin privileges to
make this change on Unix/Linux. Also note that some versions of
Windows XP automatically start IIS on port 80. So, if you use XP and
want to use port 80 for Tomcat, you may need to disable IIS (see the
Administrative Tools section of the Control Panel).
To change the port, edit install_dir/conf/server.xml and change the
port attribute of the Connector element from 8080 to 80, yielding a
result similar to that below.
<Connector port="80" ...
maxThreads="150" ...
to
<Context reloadable="true">
Since servlets and JSP are not part of the Java 2 platform, standard
edition, you have to identify the servlet classes to the compiler. The
server already knows about the servlet classes, but the compiler (i.e.,
javac) you use for development probably doesn't. So, if you don't set
your CLASSPATH, attempts to compile servlets, tag libraries, filters, Web
app listeners, or other classes that use the servlet and JSP APIs will
fail with error messages about unknown classes. Here are the
standard Tomcat locations:
install_dir/common/lib/servlet-api.jar
install_dir/common/lib/jsp-api.jar
Note that these examples represent only one approach for setting the
CLASSPATH. Many Java integrated development environments have
global or project-specific settings that accomplish the same result. But
these settings are totally IDE-specific and won't be discussed here.
Another alternative is to make a .bat file or ant build script whereby
-classpath ... is automatically appended onto calls to javac.
Windows NT/2000/XP. You could use the autoexec.bat file as above,
but a more common approach is to use system settings. On WinXP, go
to the Start menu and select Control Panel, then System, then the
Advanced tab, then the Environment Variables button. On
Win2K/WinNT, go to the Start menu and select Settings, then Control
Panel, then System, then Environment. Either way, enter the
CLASSPATH value from the previous bullet.
Please don't send me email about unresolved symbol errors until you
have confirmed that HelloServlet3.java and ServletUtilities.java are in
a subdirectory called coreservlets, and that the directory above this is
in the CLASSPATH.
Once you compile HelloServlet3.java (which will automatically cause
ServletUtilities.java to be compiled), copy (don't move!) the entire
coreservlets subdirectory from your development location to
install_dir/webapps/ROOT/WEB-INF/classes. Then, access the servlet
with the URL http://localhost/servlet/coreservlets.HelloServlet3 (again,
note that it is a dot, not a slash, between the package name and the
servlet name). You should get a simple HTML page that says "Hello
(3)".
Establish a Simplified
Deployment Method
OK, so you have a development directory. You can compile servlets
with or without packages. You know which directory the servlet
classes belong in. You know the URL that should be used to access
them. (Actually, http://hostname/servlet/ServletName is just the
default URL; you can also use the web.xml file to customize that URL;
for details, see Section 2.11 of the book.) But how do you move the
class files from the development directory to the deployment
directory? Copying each one by hand every time is tedious and error
prone. Once you start using Web applications, copying individual files
becomes even more cumbersome.
There are several options to simplify the process. Here are a few of
the most popular ones. If you are just beginning with servlets and JSP,
you probably want to start with the first option and use it until you
become comfortable with the development process. Note that I do not
list the option of putting your code directly in the server's deployment
directory. Although this is one of the most common choices among
beginners, it scales so poorly to advanced tasks that I recommend
you steer clear of it from the start.
1. Copy to a shortcut or symbolic link.
2. Use the -d option of javac.
3. Let your IDE take care of deployment.
4. Use ant or a similar tool.
By default, the Java compiler (javac) places class files in the same
directory as the source code files from which they came. However,
javac has an option (-d) that lets you designate a different location for
the class files. You need only specify the top-level directory for class
files--javac will automatically put packaged classes in subdirectories
that match the package names. So, for example, I could compile the
HelloServlet2 servlet as follows (line break added only for clarity; omit it
in real life).
javac -d install_dir/webapps/ROOT/WEB-INF/classes
HelloServlet2.java
You could even make a Windows batch file or Unix shell script or alias
that makes a command like servletc expand to javac -d
install_dir/.../classes. See
http://java.sun.com/j2se/1.4/docs/tooldocs/win32/javac.html for more
details on -d and other javac options.
An advantage of this approach is that it requires no manual copying of
class files. Furthermore, the exact same command can be used for
classes in different packages since javac automatically puts the class
files in subdirectories matching the package names.
The main disadvantage of this approach is that it applies only to Java
class files; it won't work for deploying HTML and JSP pages, much less
entire Web applications.
3. Let Your IDE Take Care of Deployment
Most servlet- and JSP-savvy development environments (e.g., Eclipse,
Borland JBuilder, Sun Java Studio Creator) have options that let you
tell the IDE where to deploy class files for your project. Then, when
you tell the IDE to build the project, the class files are automatically
deployed to the proper location (package-specific subdirectories and
all).
An advantage of this approach, at least in most IDEs, is that it can
deploy HTML and JSP pages and even entire Web applications, not just
Java class files. A disadvantage is that it is an IDE-specific technique
and thus is not portable across systems.
Use default Web application (ROOT Use a custom Web application (on
on Tomcat) Tomcat, a directory in
Use default URLs install_dir/webapps with structure
similar to ROOT)
(http:///servlet/ServletName)
Register custom URLs in WEB-
Advantages
INF/web.xml
o Simpler. Just drop the servlet
Advantages
in the right location and test
o URLs look better. No class
immediately. This is far easier
than editing web.xml every names in the URLs.
time you create a new servlet.
o Advanced features (init
o Can test without restarting params, security, filters,
server or editing web.xml etc.) depend on your using
(although Tomcat 5.5 usually registered URLs. The
notices when web.xml has invoker servlet is just a
been modified, and reloads the convenience for initial
Web app automatically when testing; never use it for real
it has) applications!
Using custom Web apps involves the following steps:
1. Making a directory whose structure mirrors the structure of the default Web
application.
2. Updating your CLASSPATH
3. Using the directory name in the URL
4. Using web.xml to assign custom URLs
1. Make a directory whose structure
mirrors the structure of the default
Web application.
HTML, JSP, images, style sheets, and other regular Web content goes in the top-
level directory (or any subdirectory other than WEB-INF or META-INF).
The web.xml file goes in the WEB-INF subdirectory
Servlets and other classes go either in WEB-INF/classes (unpackaged servlets)
or a subdirectory of WEB-INF/classes that matches the package name.
On Tomcat, entire directory is deployed in install_dir/webapps. One of the
simplest approaches is to create the Web app in your development directory
(e.g., C:\Servlets+JSP), then copy the entire directory to install_dir/webapps
whenever you want to test it. Simplify the process by keeping a shortcut to
install_dir/webapps in C:\Servlets+JSP.
Note that you can also deploy using WAR files instead of regular
directories. A WAR file is just a JAR file (which is just a ZIP file) with a
.war extension. You can create WAR files using jar, WinZip, or the
Windows XP "Create Compressed Folder" R-Mouse option. If you use
WAR files, a directory such as myWebApp should become
myWebApp.war, and the top-level directory within the WAR file should
be WEB-INF (i.e., do not repeat myWebApp within the WAR file).