Web应用开发好后,若想供外界访问,需要把web应用交给web服务器管理,这个过程称之为配置虚似目录。
<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"去配置的。