Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame^] | 1 | <html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Application Developer's Guide (6.0.39) - Deployment</title><meta name="author" content="Craig R. McClanahan"><style type="text/css" media="print">
|
| 2 | .noPrint {display: none;}
|
| 3 | td#mainBody {width: 100%;}
|
| 4 | </style></head><body bgcolor="#ffffff" text="#000000" link="#525D76" alink="#525D76" vlink="#525D76"><table border="0" width="100%" cellspacing="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img src="../images/tomcat.gif" align="right" alt="
|
| 5 | The Apache Tomcat Servlet/JSP Container
|
| 6 | " border="0"></a></td><td><h1><font face="arial,helvetica,sanserif">Apache Tomcat 6.0</font></h1><font face="arial,helvetica,sanserif">Version 6.0.39, Jan 27 2014</font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img src="../images/asf-logo.gif" align="right" alt="Apache Logo" border="0"></a></td></tr></table><table border="0" width="100%" cellspacing="4"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr noshade="noshade" size="1"></td></tr><tr><!--LEFT SIDE NAVIGATION--><td width="20%" valign="top" nowrap="nowrap" class="noPrint"><p><strong>Links</strong></p><ul><li><a href="../index.html">Docs Home</a></li></ul><p><strong>Contents</strong></p><ul><li><a href="index.html">Contents</a></li><li><a href="introduction.html">Introduction</a></li><li><a href="installation.html">Installation</a></li><li><a href="deployment.html">Deployment</a></li><li><a href="source.html">Source Code</a></li><li><a href="processes.html">Processes</a></li><li><a href="sample/">Example App</a></li></ul></td><!--RIGHT SIDE MAIN BODY--><td width="80%" valign="top" align="left" id="mainBody"><h1>Application Developer's Guide</h1><h2>Deployment</h2><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Table of Contents"><!--()--></a><a name="Table_of_Contents"><strong>Table of Contents</strong></a></font></td></tr><tr><td><blockquote>
|
| 7 | <ul><li><a href="#Background">Background</a></li><li><a href="#Standard_Directory_Layout">Standard Directory Layout</a></li><li><a href="#Shared_Library_Files">Shared Library Files</a></li><li><a href="#Web_Application_Deployment_Descriptor">Web Application Deployment Descriptor</a></li><li><a href="#Tomcat_Context_Descriptor">Tomcat Context Descriptor</a></li><li><a href="#Deployment_With_Tomcat_6">Deployment With Tomcat 6</a></li></ul>
|
| 8 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Background"><strong>Background</strong></a></font></td></tr><tr><td><blockquote>
|
| 9 |
|
| 10 | <p>Before describing how to organize your source code directories,
|
| 11 | it is useful to examine the runtime organization of a web application.
|
| 12 | Prior to the Servlet API Specification, version 2.2, there was little
|
| 13 | consistency between server platforms. However, servers that conform
|
| 14 | to the 2.2 (or later) specification are required to accept a
|
| 15 | <em>Web Application Archive</em> in a standard format, which is discussed
|
| 16 | further below.</p>
|
| 17 |
|
| 18 | <p>A web application is defined as a hierarchy of directories and files
|
| 19 | in a standard layout. Such a hierarchy can be accessed in its "unpacked"
|
| 20 | form, where each directory and file exists in the filesystem separately,
|
| 21 | or in a "packed" form known as a Web ARchive, or WAR file. The former format
|
| 22 | is more useful during development, while the latter is used when you
|
| 23 | distribute your application to be installed.</p>
|
| 24 |
|
| 25 | <p>The top-level directory of your web application hierarchy is also the
|
| 26 | <em>document root</em> of your application. Here, you will place the HTML
|
| 27 | files and JSP pages that comprise your application's user interface. When the
|
| 28 | system administrator deploys your application into a particular server, he
|
| 29 | or she assigns a <em>context path</em> to your application (a later section
|
| 30 | of this manual describes deployment on Tomcat). Thus, if the
|
| 31 | system administrator assigns your application to the context path
|
| 32 | <code>/catalog</code>, then a request URI referring to
|
| 33 | <code>/catalog/index.html</code> will retrieve the <code>index.html</code>
|
| 34 | file from your document root.</p>
|
| 35 |
|
| 36 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Standard Directory Layout"><!--()--></a><a name="Standard_Directory_Layout"><strong>Standard Directory Layout</strong></a></font></td></tr><tr><td><blockquote>
|
| 37 |
|
| 38 | <p>To facilitate creation of a Web Application Archive file in the required
|
| 39 | format, it is convenient to arrange the "executable" files of your web
|
| 40 | application (that is, the files that Tomcat actually uses when executing
|
| 41 | your app) in the same organization as required by the WAR format itself.
|
| 42 | To do this, you will end up with the following contents in your
|
| 43 | application's "document root" directory:</p>
|
| 44 | <ul>
|
| 45 | <li><strong>*.html, *.jsp, etc.</strong> - The HTML and JSP pages, along
|
| 46 | with other files that must be visible to the client browser (such as
|
| 47 | JavaScript, stylesheet files, and images) for your application.
|
| 48 | In larger applications you may choose to divide these files into
|
| 49 | a subdirectory hierarchy, but for smaller apps, it is generally
|
| 50 | much simpler to maintain only a single directory for these files.
|
| 51 | <br><br></li>
|
| 52 | <li><strong>/WEB-INF/web.xml</strong> - The <em>Web Application Deployment
|
| 53 | Descriptor</em> for your application. This is an XML file describing
|
| 54 | the servlets and other components that make up your application,
|
| 55 | along with any initialization parameters and container-managed
|
| 56 | security constraints that you want the server to enforce for you.
|
| 57 | This file is discussed in more detail in the following subsection.
|
| 58 | <br><br></li>
|
| 59 | <li><strong>/WEB-INF/classes/</strong> - This directory contains any Java
|
| 60 | class files (and associated resources) required for your application,
|
| 61 | including both servlet and non-servlet classes, that are not combined
|
| 62 | into JAR files. If your classes are organized into Java packages,
|
| 63 | you must reflect this in the directory hierarchy under
|
| 64 | <code>/WEB-INF/classes/</code>. For example, a Java class named
|
| 65 | <code>com.mycompany.mypackage.MyServlet</code>
|
| 66 | would need to be stored in a file named
|
| 67 | <code>/WEB-INF/classes/com/mycompany/mypackage/MyServlet.class</code>.
|
| 68 | <br><br></li>
|
| 69 | <li><strong>/WEB-INF/lib/</strong> - This directory contains JAR files that
|
| 70 | contain Java class files (and associated resources) required for your
|
| 71 | application, such as third party class libraries or JDBC drivers.</li>
|
| 72 | </ul>
|
| 73 |
|
| 74 | <p>When you install an application into Tomcat (or any other
|
| 75 | 2.2/2.3-compatible server), the classes in the <code>WEB-INF/classes/</code>
|
| 76 | directory, as well as all classes in JAR files found in the
|
| 77 | <code>WEB-INF/lib/</code> directory, are made visible to other classes
|
| 78 | within your particular web application. Thus, if
|
| 79 | you include all of the required library classes in one of these places (be
|
| 80 | sure to check licenses for redistribution rights for any third party libraries
|
| 81 | you utilize), you will simplify the installation of your web application --
|
| 82 | no adjustment to the system class path (or installation of global library
|
| 83 | files in your server) will be necessary.</p>
|
| 84 |
|
| 85 | <p>Much of this information was extracted from Chapter 9 of the Servlet
|
| 86 | API Specification, version 2.3, which you should consult for more details.</p>
|
| 87 |
|
| 88 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Shared Library Files"><!--()--></a><a name="Shared_Library_Files"><strong>Shared Library Files</strong></a></font></td></tr><tr><td><blockquote>
|
| 89 |
|
| 90 | <p>Like most servlet containers, Tomcat 6 also supports mechanisms to install
|
| 91 | library JAR files (or unpacked classes) once, and make them visible to all
|
| 92 | installed web applications (without having to be included inside the web
|
| 93 | application itself). The details of how Tomcat locates and shares such
|
| 94 | classes are described in the
|
| 95 | <a href="../class-loader-howto.html">Class Loader HOW-TO</a> documentation.
|
| 96 | The location commonly used within a Tomcat 6 installation for shared code is
|
| 97 | <strong>$CATALINA_HOME/lib</strong>. JAR files placed here are visible both to
|
| 98 | web applications and internal Tomcat code. This is a good place to put JDBC
|
| 99 | drivers that are required for both your application or internal Tomcat use
|
| 100 | (such as for a JDBCRealm).</p>
|
| 101 |
|
| 102 | <p>Out of the box, a standard Tomcat 6 installation includes a variety
|
| 103 | of pre-installed shared library files, including:</p>
|
| 104 | <ul>
|
| 105 | <li>The <em>Servlet 2.5</em> and <em>JSP 2.1</em> APIs that are fundamental
|
| 106 | to writing servlets and JavaServer Pages.<br><br></li>
|
| 107 | </ul>
|
| 108 |
|
| 109 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Web Application Deployment Descriptor"><!--()--></a><a name="Web_Application_Deployment_Descriptor"><strong>Web Application Deployment Descriptor</strong></a></font></td></tr><tr><td><blockquote>
|
| 110 |
|
| 111 | <p>As mentioned above, the <code>/WEB-INF/web.xml</code> file contains the
|
| 112 | Web Application Deployment Descriptor for your application. As the filename
|
| 113 | extension implies, this file is an XML document, and defines everything about
|
| 114 | your application that a server needs to know (except the <em>context path</em>,
|
| 115 | which is assigned by the system administrator when the application is
|
| 116 | deployed).</p>
|
| 117 |
|
| 118 | <p>The complete syntax and semantics for the deployment descriptor is defined
|
| 119 | in Chapter 13 of the Servlet API Specification, version 2.3. Over time, it
|
| 120 | is expected that development tools will be provided that create and edit the
|
| 121 | deployment descriptor for you. In the meantime, to provide a starting point,
|
| 122 | a <a href="web.xml.txt">basic web.xml file</a>
|
| 123 | is provided. This file includes comments that describe the purpose of each
|
| 124 | included element.</p>
|
| 125 |
|
| 126 | <p><strong>NOTE</strong> - The Servlet Specification includes a Document
|
| 127 | Type Descriptor (DTD) for the web application deployment descriptor, and
|
| 128 | Tomcat 6 enforces the rules defined here when processing your application's
|
| 129 | <code>/WEB-INF/web.xml</code> file. In particular, you <strong>must</strong>
|
| 130 | enter your descriptor elements (such as <code><filter></code>,
|
| 131 | <code><servlet></code>, and <code><servlet-mapping></code> in
|
| 132 | the order defined by the DTD (see Section 13.3).</p>
|
| 133 |
|
| 134 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Tomcat Context Descriptor"><!--()--></a><a name="Tomcat_Context_Descriptor"><strong>Tomcat Context Descriptor</strong></a></font></td></tr><tr><td><blockquote>
|
| 135 |
|
| 136 | <p>A /META-INF/context.xml file can be used to define Tomcat specific
|
| 137 | configuration options, such as an access log, data sources, session manager
|
| 138 | configuration and more. This XML file must contain one Context element, which
|
| 139 | will be considered as if it was the child of the Host element corresponding
|
| 140 | to the Host to which the web application is being deployed. The
|
| 141 | <a href="../config/index.html">Tomcat configuration documentation</a> contains
|
| 142 | information on the Context element.</p>
|
| 143 |
|
| 144 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Deployment With Tomcat 6"><!--()--></a><a name="Deployment_With_Tomcat_6"><strong>Deployment With Tomcat 6</strong></a></font></td></tr><tr><td><blockquote>
|
| 145 |
|
| 146 | <blockquote><em>
|
| 147 | <p>The description below uses the variable name $CATALINA_BASE to refer the
|
| 148 | base directory against which most relative paths are resolved. If you have
|
| 149 | not configured Tomcat 6 for multiple instances by setting a CATALINA_BASE
|
| 150 | directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME,
|
| 151 | the directory into which you have installed Tomcat 6.</p>
|
| 152 | </em></blockquote>
|
| 153 |
|
| 154 | <p>In order to be executed, a web application must be deployed on
|
| 155 | a servlet container. This is true even during development.
|
| 156 | We will describe using Tomcat 6 to provide the execution environment.
|
| 157 | A web application can be deployed in Tomcat by one of the following
|
| 158 | approaches:</p>
|
| 159 | <ul>
|
| 160 | <li><em>Copy unpacked directory hierarchy into a subdirectory in directory
|
| 161 | <code>$CATALINA_BASE/webapps/</code></em>. Tomcat will assign a
|
| 162 | context path to your application based on the subdirectory name you
|
| 163 | choose. We will use this technique in the <code>build.xml</code>
|
| 164 | file that we construct, because it is the quickest and easiest approach
|
| 165 | during development. Be sure to restart Tomcat after installing or
|
| 166 | updating your application.
|
| 167 | <br><br></li>
|
| 168 | <li><em>Copy the web application archive file into directory
|
| 169 | <code>$CATALINA_BASE/webapps/</code></em>. When Tomcat is started, it will
|
| 170 | automatically expand the web application archive file into its unpacked
|
| 171 | form, and execute the application that way. This approach would typically
|
| 172 | be used to install an additional application, provided by a third party
|
| 173 | vendor or by your internal development staff, into an existing
|
| 174 | Tomcat installation. <strong>NOTE</strong> - If you use this approach,
|
| 175 | and wish to update your application later, you must both replace the
|
| 176 | web application archive file <strong>AND</strong> delete the expanded
|
| 177 | directory that Tomcat created, and then restart Tomcat, in order to reflect
|
| 178 | your changes.
|
| 179 | <br><br></li>
|
| 180 | <li><em>Use the Tomcat 6 "Manager" web application to deploy and undeploy
|
| 181 | web applications</em>. Tomcat 6 includes a web application, deployed
|
| 182 | by default on context path <code>/manager</code>, that allows you to
|
| 183 | deploy and undeploy applications on a running Tomcat server without
|
| 184 | restarting it. See the administrator documentation (TODO: hyperlink)
|
| 185 | for more information on using the Manager web application.<br><br></li>
|
| 186 | <li><em>Use "Manager" Ant Tasks In Your Build Script</em>. Tomcat 6
|
| 187 | includes a set of custom task definitions for the <code>Ant</code>
|
| 188 | build tool that allow you to automate the execution of commands to the
|
| 189 | "Manager" web application. These tasks are used in the Tomcat deployer.
|
| 190 | <br><br></li>
|
| 191 | <li><em>Use the Tomcat Deployer</em>. Tomcat 6 includes a packaged tool
|
| 192 | bundling the Ant tasks, and can be used to automatically precompile JSPs
|
| 193 | which are part of the web application before deployment to the server.
|
| 194 | <br><br></li>
|
| 195 | </ul>
|
| 196 |
|
| 197 | <p>Deploying your app on other servlet containers will be specific to each
|
| 198 | container, but all containers compatible with the Servlet API Specification
|
| 199 | (version 2.2 or later) are required to accept a web application archive file.
|
| 200 | Note that other containers are <strong>NOT</strong> required to accept an
|
| 201 | unpacked directory structure (as Tomcat does), or to provide mechanisms for
|
| 202 | shared library files, but these features are commonly available.</p>
|
| 203 |
|
| 204 | </blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr noshade="noshade" size="1"></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font color="#525D76" size="-1"><em>
|
| 205 | Copyright © 1999-2014, Apache Software Foundation
|
| 206 | </em></font></div></td></tr></table></body></html> |