Tomcat部署web应用的三种方式

时间:2023-03-09 00:30:06
Tomcat部署web应用的三种方式

原文:http://my.oschina.net/sunchp/blog/90235

一:相关概念

CATALINA_HOME:tomcat安装目录

CATALINA_BASE:tomcat工作目录

Context:一个web应用

二:部署方法(以PetWeb项目为例说明,PetWeb目录假设是C:/PetWeb)

①$CATALINA_BASE/webapps(一般方法)

将PetWeb目录拷贝到$CATALINA_BASE/webapps下,然后启动服务器就可以了,Tomcat启动时将自动加载应用。

访问地址如下:http://localhost:8080/PetWeb/

webapps是tomcat默认存放web应用的目录webapps下有一个"ROOT"(全部大写)目录,默认情况下访问http://localhost:8080/就是访问的ROOT目录的应用

webapps和ROOT的配置和修改在conf目录下的server.xml中配置。

②修改Server.xml文件部署(不推荐)

这种方式可以不必将PetWeb目录拷贝到webapps下。方法如下,更改$CATALINA_BASE/conf/server.xml文件,在Host标签内建一个Context,内容如下:

1
<Context  path ="/Pet"  reloadable ="false"  docBase ="C:/PetWeb"  workDir ="d:/Mywebapps/emp"  />

path:表示访问的路径;如上述例子中,访问该应用程序地址如下:http://localhost:8080/Pet/

reloadable:表示可以在运行时在classes与lib文件夹下自动加载类包。其中reloadable="false"表示当应用程序 中的内容发生更改之后服务器不会自动加载,这个属性在开发阶段通常都设为true,方便开发,在发布阶段应该设置为false,提高应用程序的访问速度。

docbase:表示应用程序的路径,注意斜杠的方向“/”。 docBase可以使用绝对路径,也可以使用相对路径,相对路径相对于webapps。

workdir:表示缓存文件的放置地址。

③创建一个Context的xml文件(推荐)

文件名任意取,最好是跟你的web应用相同便于管理,注意此文件名将作为Context中的path属性值,不管文件里的path属性值如何设置也是无效的,内容如下:

<Context    docBase ="C:/PetWeb"  debug ="0"  privileged ="true"  reloadable ="false"  >  

</Context>

将该xml文件放在:

$CATALINA_BASE/conf/[enginename]/[hostname]/

三:删除web应用

删除web应用就是删除以上三处地方的相关文件内容:

删除webapps下相应的文件夹;server.xml中相应的Context;$CATALINA_BASE/conf/[enginename]/[hostname]/下相应的xml文件。

-------------------------------------------------------------------------------------------

PS:tomcat关于Context的官方说明(web应用的xml描述文件:context.xml)

In talking about deployment of web applications, the concept of a Context is required to be understood. A Context is what Tomcat calls a web application.
In order to configure a Context within Tomcat a Context Descriptor is required. A Context Descriptor is simply an XML file that contains Tomcat related configuration for a Context, e.g naming resources or session manager configuration. In earlier versions of Tomcat the content of a Context Descriptor configuration was often stored within Tomcat's primary configuration file server.xml but this is now discouraged (although it currently still works).
Context Descriptors not only help Tomcat to know how to configure Contexts but other tools such as the Tomcat Manager and TCD often use these Context Descriptors to perform their roles properly.
The locations for Context Descriptors are:

$CATALINA_BASE/conf/[enginename]/[hostname]/context.xml
$CATALINA_BASE/webapps/[webappname]/META-INF/context.xml

Files in (1) are named [webappname].xml but files in (2) are named context.xml. If a Context Descriptor is not provided for a Context, Tomcat configures the Context using default values.