JavaLife
天之道,利而不害;圣人之道,为而不争;信言不美,美言不信;善者不辩,辩者不善;知者不博,博者不知!
posts - 8,comments - 15,trackbacks - 0

                                                 Configuring & Using Apache Tomcat
原文地址:http://www.coreservlets.com/Apache-Tomcat-Tutorial/

                                                               Introduction

CSAJSP
Core Servlets & JSP 2/E
MSAJSP
More Servlets & JSP
Following is a guide to installing and configuring Apache Tomcat 5.5 for use as a standalone Web server (for development) that supports servlets 2.4 and JSP 2.0. (Note: Apache Tomcat is sometimes referred to as "Jakarta Tomcat" since the Apache Java effort is known as "The Jakarta Project"). This Tomcat tutorial covers version 5.5.9; for coverage of older Tomcat versions (Apache Tomcat 5.0.x and Apache Tomcat 4.0.x), please see the Apache Tomcat 5.0 and 4.0 tutorial.

Using Tomcat as a deployment server or integrating Tomcat as a plugin within the regular Apache server or a commercial Web server is more complicated than what is described in this tutorial. Although such integration is valuable for a deployment scenario (see http://jakarta.apache.org/tomcat/tomcat-5.5-doc/), my goal here is to show how to use Tomcat as a development server on your desktop. Regardless of what deployment server you use, you'll want a standalone server on your desktop to use for development.

The examples here assume you are using Windows, but they can be easily adapted for Linux, Solaris, and other versions of Unix. I've gotten many reports of successful use on MacOS X, but don't know the setup details. Except when I refer to specific Windows paths (e.g., C:\blah\blah), I use URL-style forward slashes for path separators (e.g., install_dir/webapps/ROOT). Adapt as necessary.

The information here is adapted from the introductory setup and configuration chapter of Core Servlets and JavaServer Pages, 2nd Edition, Volume 1 from Sun Microsystems Press and Prentice Hall. For the book table of contents, index, source code, etc., please see http://volume1.coreservlets.com/. For information on training courses (either at public venues or on-site at your company) taught by the author of the book and this Apache Tomcat tutorial, please see this servlet, JSP, Struts, JSF, and Java training courses page. To report errors or omissions in this writeup or to inquire about on-site training courses on servlets, JSP, Struts, JSF, and Java, please contact Marty Hall at hall@coreservlets.com.

Executive Summary

JSP, Servlet, Struts, & JSF Training
Looking for hands-on training? Tired of inexperienced instructors teaching someone else's course materials? Want a choice of public or in-house courses? Check out these servlet, JSP, Struts, and JSF training courses.
Distinctive features:
Personally developed and taught by Marty Hall, an experienced developer, award-winning instructor, 4-time JavaOne speaker, and author of Core Servlets and JSP, More Servlets and JSP, and this Apache Tomcat tutorial.
A choice of on-site training courses at your organization or training courses at public venues.
Well-tested, having been used by Marty for courses for more than 100 different organizations in the USA, Canada, Australia, Japan, Puerto Rico, and the Philippines.
I give extremely detailed instructions in the following sections. If you're pretty experienced and just want a short summary, this section will probably be enough for you. You can also download a preconfigured Apache Tomcat version that incorporates all of the modifications of this tutorial and includes a sample development directory, shortcuts, and autoexec.bat file.

  1. Install the JDK. Make sure JDK 1.4 or 1.5 is installed and your PATH is set so that both "java -version" and "javac -help" give a result.
  2. Configure Tomcat.
    1. Download the software. Go to http://jakarta.apache.org/site/binindex.cgi#tomcat and download and unpack the zip file for the current release build of Tomcat 5.5. If you use Tomcat 5.5 with JDK 1.4 (recommended), you have to also download and unpack the "Compat" zip file. Also see the preconfigured Apache Tomcat version.
    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. Not necessary on recent version 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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. If not, I recommend JDK 1.4 instead of JDK 1.5 so that you don't limit portability by accidentally using Java 1.5 features in your code. See the following sites for download and installation information.

  • 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 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 MacOS: http://developer.apple.com/java/.

Once you've installed Java, confirm that everything including your PATH is configured properly by opening a DOS window and typing "java -version" and "javac -help". You should see a real result both times, not an error message about an unknown command. Or, if you use an IDE, compile and run a simple program to confirm that the IDE knows where you installed Java.

Configure Tomcat

JSP, Servlet, Struts, & JSF Training
Looking for hands-on training? Tired of inexperienced instructors teaching someone else's course materials? Want a choice of public or in-house courses? Check out these servlet, JSP, Struts, and JSF training courses.
Distinctive features:
Personally developed and taught by Marty Hall, an experienced developer, award-winning instructor, 4-time JavaOne speaker, and author of Core Servlets and JSP, More Servlets and JSP, and this Apache Tomcat tutorial.
A choice of on-site training courses at your organization or training courses at public venues.
Well-tested, having been used by Marty for courses for more than 100 different organizations in the USA, Canada, Australia, Japan, Puerto Rico, and the Philippines.
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)

Details of each step are given below. If Tomcat is already running, restart it after performing these steps.

1. Download the Apache Tomcat Software

Go to http://jakarta.apache.org/site/binindex.cgi#tomcat and download the zip file for the current release build of Jakarta Tomcat 5.5. If you need to use an older version of Tomcat to be compatible with an app server such as BEA WebLogic 8.1 that does not support JSP 2.0, please see the older Apache Tomcat 5.0 and 4.0 tutorial.

Note that if you use Tomcat 5.5 with JDK 1.4 (the recommended setup for most users), you have to download both the main zip file and the "Compat" zip file. If you use Tomcat 5.5 with JDK 1.5, you do not need the "Compat" zip file, but it is better to use JDK 1.4 (unless you will deploy on JDK 1.5 only) so that you do not limit portability by accidentally using JDK 1.5 constructs in your Java code.

Save the zip file(s) on your PC and unzip into a location of your choice. You specify the top-level directory (e.g., C:\) and the zip file has embedded subdirectories (e.g., jakarta-tomcat-5.5.9). Thus, C:\jakarta-tomcat-5.5.9 is a common resultant installation directory. Note: from this point forward, I'll refer to that location as install_dir. For Windows, there is also a .exe installer; I prefer the .zip file, but see the .exe installer section for notes on the differences between the two.

Alternatively, you can use my preconfigured Jakarta Tomcat version. This version already has the port changed to 80, servlet reloading enabled, the invoker servlet turned on, and the "Compat" package included. It also comes with a sample development directory and autoexec.bat file.

2. Set the JAVA_HOME Variable

Next, you must set the JAVA_HOME environment variable to tell Tomcat where to find Java. Failing to properly set this variable prevents Tomcat from compiling JSP pages. This variable should list the base JDK installation directory, not the bin subdirectory. For example, on almost any version of Windows, if you installed the JDK in C:\j2sdk1.4.2_08, you might put the following line in your C:\autoexec.bat file.

  set JAVA_HOME=C:\j2sdk1.4.2_08

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" minSpareThreads="25" ...

You can also:

4. Turn on Servlet Reloading

The next step is to tell Tomcat to check the modification dates of the class files of requested servlets, and reload ones that have changed since they were loaded into the server's memory. This slightly degrades performance in deployment situations, so is turned off by default. However, if you fail to turn it on for your development server, you'll have to restart the server every time you recompile a servlet that has already been loaded into the server's memory. Since this tutorial discusses the use of Tomcat for development, this change is strongly recommended.

To turn on servlet reloading, edit Edit install_dir/conf/context.xml and change

  <Context>

to

  <Context reloadable="true">

You can also:

5. Enable the Invoker Servlet

The invoker servlet lets you run servlets without first making changes to your Web application's deployment descriptor (i.e., the WEB-INF/web.xml file). Instead, you just drop your servlet into WEB-INF/classes and use the URL http://host/servlet/ServletName (or http://host/webAppName/servlet/ServletName once you start using your own Web applications; see Section 2.11 of the book for details on Web apps). The invoker servlet is extremely convenient when you are learning and even when you are doing your initial development. You almost certainly want to enable it when learning, but you should disable it again before deploying any real applications.

To enable the invoker servlet, uncomment the following servlet and servlet-mapping elements in install_dir/conf/web.xml. Do not confuse this Apache Tomcat-specific web.xml file with the standard one that goes in the WEB-INF directory of each Web application.

    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>
          org.apache.catalina.servlets.InvokerServlet
        </servlet-class>
        ...
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>

You can also:

6. Change DOS Memory Settings (Win 98/Me Only)

If you use Windows 98/Me, you may also have to change the DOS memory settings for the startup and shutdown scripts. If you get an "Out of Environment Space" error message when you start the server, you will need to 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. This step is not necessary in more recent versions of Windows.

7. Set the CATALINA_HOME Variable (Optional)

If you are going to make copies of the Tomcat startup or shutdown scripts (e.g., startup.bat and shutdown.bat), it is also helpful to set the CATALINA_HOME environment variable to refer to the top-level directory of the Apache Tomcat installation (e.g., C:\jakarta-tomcat-5.5.9<%2

posted on 2006-02-28 12:31 JavaLife 阅读(2197) 评论(0)  编辑  收藏 所属分类: Tomcat

只有注册用户登录后才能发表评论。


网站导航: