1-web應用和虛似目錄的映射
Web應用開發好後,若想供外界訪問,需要把web應用所在目錄交給web服務器管理,這個過程稱之爲虛擬目錄的映射。
Tomcat 6幫助文檔(文檔可以在C:\apache-tomcat-6.0.20\webapps\docs目錄下的index.html中找到)中所提到的5種配置方式,這裏只說第三種和第五種。
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:
1.In the $CATALINA_BASE/conf/context.xml file: the Context element information will be loaded by all webapps.
2.In the $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host.
3.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.
在C:\apache-tomcat-6.0.20\conf\Catalina\localhost目錄下建文檔a.xml,內容爲:<Context docBase="c:\news" /> 這種方法服務器不需要重啓
http://localhost:8080/a/1.html
複製a.xml爲b.xml http://localhost:8080/b/1.html
同樣方式在這個目錄下配置一個ROOT.xml文檔,這個缺省的訪問路徑,配置完後要重啓服務器,覆蓋原來的缺省目錄。
4.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.
5.Inside a Host element in the main conf/server.xml.
在C:\apache-tomcat-6.0.20\conf目錄下的server.xml中配置如下:
<Context path="/itcast" docBase="c:\news"/>
但是這種配置不好,需要重啓服務器。
一般在實際開發中使用第三種方式。
2.配置虛擬主機
在C:\apache-tomcat-6.0.20\conf目錄下的server.xml中配置如下:
<Host name="www.sina.com" appBase="c:\sina">
<Context path="/mail" docBase="c:\sina\mail"/>
</Host>
可以通過http://www.sina.com/mail/1,html來訪問,但是我是在主機的主機上做的,所以這樣做是不能訪問的,瀏覽器會去找本地的hosts文件,發現沒有這樣的映射,然後去找DNS服務器進行域名解析。所以我們可以去hosts文件中進行配置。hosts文件位置:C:\Windows\System32\drivers\etc\hosts
3.配置網站主頁
WEB-INF目錄下的web.xml中輸入如下代碼:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>1.html</welcome-file> 1.html就是主頁,同時要在虛擬主機配置時path設置爲缺省值""
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>