目的:
修改Tomcat配置文件,實現在瀏覽器地址欄中輸入
www.mytest.com
時即可訪問指定項目。先上圖,然後才能清楚這篇文章到底要說什麼。
步驟:
1、進入tomcat安裝目錄中的webapps
文件夾,創建一個新的項目demo
,並添加index.html作爲要訪問的文件
index.html文件內容:
<html>
<head>
<title>Tomcat Test</title>
</head>
<body>
<p>This is Tomcat Test.</p>
</body>
</html>
2、進入tomcat安裝目錄中的conf
文件夾,並用記事本打開server.xml文件,然後做出如下修改:
(1)修改訪問端口,將8080
修改爲80
,80
爲windows訪問http協議的默認端口。修改後的配置如下:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
(2)修改訪問域名,將原localhost
修改爲www.mytest.com
。修改後的配置如下:
<Host name="www.mytest.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
...
(3)在Host
標籤中添加<Context>
標籤,內容如下:
<Host name="www.mytest.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve .../>
<Context path="" docBase="/demo" debug="0" reloadable="true"></Context>
</Host>
其中:path
指定瀏覽器地址欄中要輸入的項目名稱,空字符串表示不用輸入項目名稱;docBase
指定要訪問項目的物理路徑,可相對路徑,也可絕對路徑,在此處/demo
即爲相對路徑,表示的是要訪問的項目是webapps下的demo項目。
(4)最後 一步,也是最關鍵的一步。少了這一步,可能還是訪問不了www.mytest.com
。還需要修改hosts
文件。
hosts文件所在路徑:
C:\Windows\System32\drivers\etc\hosts
,windows7系統可能沒有這個文件,可以自行創建一個。
host文件內容如下:
127.0.0.1 www.mytest.com
就這一行就可以了
啓動Tomcat看看效果吧~