配置虚似目录

来源:互联网 发布:在家编程赚钱 编辑:程序博客网 时间:2024/05/13 00:28

Web应用开发好后,若想供外界访问,需要把web应用交给web服务器管理,这个过程称之为配置虚似目录。

 

1. tomcat服务器会自动管理webapps目录下的所有web应用,并把它映射成虚似目录。换句话说,只要把web项目放置到tomcat服务器的webapps目录中,不需要做其它设置,这个web应用就可以直接被外界访问了。
 
2. 对计算机中的任意位置的WEB应用,若想被外界访问,就需要手工通知web服务器去管理,即通知web服务器把其映射成虚似目录,这样才能供外界访问。
配置方法,在conf下的server.xml下,在host结束标签之前加入红色字体标识的一行:

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

<!--表示管理C盘下的machinecat这个应用,通过浏览器访问的路径问http://ip:端口号/hn访问-->

<Context path=“/hn" docBase="d:\hainan" />

</Host>

 

 

注意:

tomcat5是不会自动把webapps下的应用映射成虚拟目录的,得手工配置;或者是webapps下的应用中有WEB-INF目录,WEB-INF目录下有web.xml文件,这样才能自动映射。

 

因为配置完虚拟目录还要重启服务器,这样重启期间很多东西都不能访问,所以不建议在server.xml下配置虚拟目录。

在Tomcat6中,不再建议在server.xml文件中配置context元素,细节查看tomcat服务器关于context元素的说明。
IE中打开
http://localhost:8080/ ---> 点左边Documentation/Tomcat Documentation --->Reference/Configuration--->Containers/Context,看到有:
For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes
modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.

Context elements may be explicitly defined:
•In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.
In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.
•In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
•Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.
•Inside a Host element in the main conf/server.xml.


根据第二种方式,在apache-tomcat-6.0.20\conf\Catalina\localhost下新建一个任何名字的xml文件,比如confcontext.xml,里面写<Context docBase="d:\hainan" /> ,
那么访问http://localhost:8080/confcontext就能访问到此web应用。这样的改动是无需重启的。如果xml文件名为foo#bar.xml,那么访问路径为http://localhost:8080//foo/bar。
如果不制定访问路径,那么tomcat提供缺省的访问http://localhost:8080,也就是apache-tomcat-6.0.20\webapps\ROOT\index.html。
如果想将自己的应用配置成缺省web应用,那么confcontext.xml的名字改成ROOT,也就是ROOT.xml。把里面的某个文件映射成首页就能通过http://localhost:8080访问。到web应用/WEB-INF/web.xml中去配首页。
<welcome-file-list>
  <welcome-file>xxx.html</welcome-file>
</welcome-file-list>

tomcat自带的例子中的web.xml文件没有配首页,也能缺省访问,这个是为什么呢?因为此web.xml继承了apache-tomcat-6.0.20\conf下的web.xml。
记住:配置某个web应用,都是通过"web应用/WEB-INF/web.xml"去配置的。

 

0 0
原创粉丝点击