您的位置:首页 > 运维架构 > Tomcat

使用tomcat_2——虚拟目录映射

2016-07-11 12:04 429 查看
参看:http://www.cnblogs.com/xdp-gacl/p/3734395.html

Tomcat服务器虚拟目录的映射方式

第一种:让tomcat服务器自动映射(推荐)

tomcat服务器会自动管理webapps目录下的所有web应用,并把它映射成虚似目录。换句话说,tomcat服务器webapps目录中的web应用,外界可以直接访问。

第二种:添加自己的xml

   参考Tomcat服务器文档:

  In individual files (with a ".xml" extension) in the
$CATALINA_BASE/conf/[enginename]/[hostname]/
directory. The context path and version will be derived from the base name of the file (the file name less the .xml
extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.

  意思就是:在tomcat服务器的\conf\Catalina\localhost目录下添加一个以xml作为扩展名的文件,xml文件的名字可以任意取,比如下面的aa.xml,注意这一句话"The context path and version will be derived from the base name of the file",这一句话的意思翻译过来就是"context元素的path属性源自于是这个xml文件的名字",上面提到过,Context元素的path属性是用来配置虚似目录的名称的,所以虚似目录的名称就是这个xml文件的名称。
  $CATALINA_BASE
指的就是tomcat服务器根目录,
[enginename]
指的是Tomcat服务器使用的引擎名称,Tomcat使用的引擎是Catalina

在aa.xml文件中添加Context元素映射JavaWeb应用,代码如下:

1 <Context docBase="F:\JavaWebDemoProject" />


这里的docBase就是源目录,也就是实际存放web应用的目录。类似于tomcat的webapp目录。
这里需要特别注意的是:aa.xml虽然可以任意命名,但是一旦名字确定后,外部访问时的url就要相应做调整,比如:http://localhost:8080/aa/

第三种:修改service.xml的host元素

        找到server.xml文件的host元素,例如在<Host></Host>这对标签加上<Contextpath="/JavaWebApp"
docBase="F:\JavaWebDemoProject"/>即可将在F盘下的JavaWebDemoProject这个JavaWeb应用映射到JavaWebApp这个虚拟目录上,JavaWebApp这个虚拟目录是由Tomcat服务器管理的,JavaWebApp是一个硬盘上不存在的目录,是我们自己随便写的一个目录,也就是虚拟的一个目录,所以称之为"虚拟目录"。

其中,Context表示上下文,代表的就是一个JavaWeb应用,Context元素有两个属性,
  Ⅰ.path:用来配置虚似目录,必须以"/"开头。
  Ⅱ.docBase:配置此虚似目录对应着硬盘上的Web应用所在目录。
注意:path部分可以随便命名,但是和上一种情况一样,注意URL与path名相关。

注意:在Tomcat6之后中,不再建议在server.xml文件中使用配置context元素的方式来添加虚拟目录的映射,因为每次修改server.xml文件后,Tomcat服务器就必须要重新启动后才能重新加载server.xml文件。在Tomcat服务器的文档http://localhost:8080/docs/config/context.html中有这样的说明:
  It is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifying theContext configuration more invasive since the main
conf/server.xml
file cannot be reloaded without restarting Tomcat.

Individual Context elements may be explicitly defined:

In an individual file at
/META-INF/context.xml
inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to
$CATALINA_BASE/conf/[enginename]/[hostname]/
and renamed to application's base
file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the
$CATALINA_BASE/conf/[enginename]/[hostname]/
directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will
always take precedence over any context.xml file packaged in the web application's META-INF directory.
Inside a
Host element in the main
conf/server.xml
.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: