tomcat虛擬目錄映射

來源:互聯網
上載者:User
安裝好tomcat後,把你的web項目copy到%TOMCAT_HOME%webapps下面就OK 了。有種更優秀方法就是設定虛擬目錄,即把項目的目錄映射到tomcat中。這樣項目不需要再放到tomcat下,而且給開發人員帶來方便,方法如下:
1、找到%TOMCAT_HOME%\conf\server.xml,(即tomcat的安裝目錄下面相關的檔案)開啟此xml檔案,在 <host> </host>之間加入如下代碼:

<Context path="" reloadable="true" debug="0" docBase="E:\workspace\test\bin" workDir="E:\workspace\test\work" crossContext="true" >
</Context>

<Context path="/test" docBase="E:\aaa" debug="0" reloadable="true"/>

    docBase是實際路徑,這裡一般寫一個絕對的磁碟路徑,當然也可以寫相對路徑,相對路徑是相對於tomcat安裝目錄的webapps目錄的,通常設定虛擬目錄的目的是將tomcat的安裝和項目的儲存分開,所以這裡建議你使用磁碟絕對路徑,並且這個磁碟路徑不時在tomcat安裝目錄下。workDir是tomcat運行是對JSP檔案做編譯後的檔案,這個檔案最好也是分開。
path為虛擬路徑,訪問時的路徑,注意:一定要加“/”。值得注意的是:path可以為"/"那麼訪問“IP:連接埠”就可以訪問到布置的工程,如果為"/test",那麼必須得訪問“IP:連接埠/test”才能訪問到,我建議採用第一種方法。不然訪問每個工程都得有個虛擬路徑,看上去不舒服。可能你會想到如果我有多個工程怎麼辦。是不是一定得取幾個虛擬路徑,如果你用一個IP的話那是肯定的。但是如果你把機器多設定幾個IP,那麼就行了,每個工程佔用一個IP,這樣階層更加清晰。
debug建議設定為0,reloadable設定為true。

其實不用那麼麻煩,只要把<Context path="/EHOP" docBase="D:\Study\Myproject\workspace\EShop\WebRoot"/> 寫在</Host>上面一行就OK了。


2、如果在測試中出現404的錯誤,如果前面配置沒有錯,可能就是在%TOMCAT_HOME%\conf\web.xml檔案中把虛擬路徑顯示目錄給禁止啦,此時可以在web.xml檔案中找到
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
將listings的value改為true,然後重新啟動tomcat,在輸入url,測試成功。


3、如果項目開發完成,準備部署在伺服器上時,記住要把第三步中web.xml檔案中參數listings的值改為false,這樣可以避免把項目的部署路徑呈現給使用者。
具體如下:
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>

</init-param>


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

Context Naming:

When autoDeploy or deployOnStartup is used then there is a close relationship between thecontext name, context path, context version and the base file name used for the WAR and/or directory that contains the web application when the WAR or directory is located in the Host's appBase. When no version is specified, the rules are: contextName = contextPath If the contextPath is a zero length string, the base name is ROOT If the contextPath is not a zero length string, the base name is the contextPath with the leading '/' removed and any remaining '/' characters in the path replaced with '#'. When a version is specified, ##version is added to the contextName and base name. To help clarify these rules, some examples are given in the following table.

Context Path Context Version Context Name Base filename
/foo None /foo foo
/foo/bar None /foo/bar foo#bar
Empty String None Empty String ROOT
/foo 42 /foo##42 foo##42
/foo/bar 42 /foo/bar##42 foo#bar##42
Empty String 42 ##42 ROOT##42

The version component is treated as a String both for performance reasons and to allow flexibility in versioning schemes. String comparisons are used to determine version order. If version is not specified, it is treated as the empty string. Therefore, foo.war will be treated as an earlier version than foo##11.war and foo##11.war will be treated as an earlier version thanfoo##2.war. If using a purely numerical versioning scheme it is recommended that zero padding is used so thatfoo##002.war is treated as an earlier version than foo##011.war.

If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment: Disable autoDeploy and deployOnStartup and define all Contexts in server.xml Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.

Defining a Context:

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 mainconf/server.xml.

Default Context elements may be defined that apply to multiple web applications. Configuration for an individual web application will override anything configured in one of these defaults. Any nested elements, e.g. <Resource> elements, that are defined in a default Context will be created once for each Context to which the default applies. They will not be shared betweenContext elements. 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.

With the exception of server.xml, files that define Context elements may only define a singleContext element.

In addition to explicitly specified Context elements, there are several techniques by which Context elements can be created automatically for you. SeeAutomatic Application Deployment and User Web Applications for more information.

To define multiple Contexts that use a single WAR file or directory, use one of the options described above for creating aContext that has a path that is not related to the base file name and create multipleContext definitions. For more infomation refer to: Virtual Hosting and Tomcat

                                                   The Context Container

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.