tomcat部署
準備java運行環境Tomcat6運行需要Java環境,需要先下載并安裝J2SE5.0以上的版本。
下載地址:http://java.sun.com/javase/downloads/index.jsp
注意:tomcat6.0已經不再需要jdk,而只要jre就可以了,因為它采用了自帶的Eclipse JDT來編譯jsp,而不是jdk。
如果JDK是zip解壓,而不是install安裝的,請設置系統環境JAVA_HOME:
在桌面上右鍵點擊【我的電腦】,選擇【屬性】命令,在出現的對話框中選擇【高級】選項卡,然后單擊【環境變量】按鈕,添加一個環境變量:JAVA_HOME,它的值為JDK所在根目錄(比如:c:\jdk,而不是c:\jdk\bin)
tomcat下載地址:http://tomcat.apache.org/
右側有從3.3到6.x各個版本的下載鏈接。
windows下tomcat的安裝很簡單,可以采用exe安裝包的方式進行安裝?;旧弦宦反_定就可以了,安裝用到了java環境,從jdk的安裝注冊表或者JAVA_HOME這個系統變量里面獲得jdk所在位置(這就是為什么非安裝的jdk需要配置JAVA_HOME的原因,當然在后面tomcat運行時也是需要的)。
安裝后的目錄如下:
c:\tomcat6.0
|---bin
|---conf
|---lib
|---logs
|---temp
|---webapps
|---work
相比之前的版本,在bin和lib這兩個目錄改動比較大,bin目錄不再有一堆的bat文件,精簡為4個文件。而jar文件統一放到新增的lib目錄下,先前版本中下面的3個目錄被刪除合并(參考tomcat5.0.28的結構):
common(含lib,classes,endorsed三個目錄)
share(含lib和classes兩個目錄)
server(含lib,classes,webapps三個目錄)
此外,examples增加了
安裝的最后一個步驟,可以選擇立即啟動;
默認情況下,是通過系統的服務進行啟動的,可以把啟動方式設置為“自動”
tomcat/bin/tomcat6 或者tomcat/bin/tomcat6 //TS//tomcat6(注意大寫!下同) 二者是等價的。
其他的參數還有:
tomcat/bin/tomcat6 //RS//tomcat6 ---啟動tomcat6服務
tomcat/bin/tomcat6 //US//tomcat6 ---更新tomcat6服務參數
tomcat/bin/tomcat6 //IS//tomcat6 ---安裝tomcat6服務
tomcat/bin/tomcat6 //DS//tomcat6 ---刪除tomcat6服務
安裝后的bin目錄共有一下幾個文件:
bootstrap.jar
tomcat-juli.jar
tomcat6.exe
tomcat6w.exe
其中tomcat6w.exe是監控tomcat運行的,可以直接運行,或者縮小到屏幕右下角成為一個小圖標。
直接運行tomcat6w.exe或者“tomcat6w //ES//” 都可以進入監控配置窗口;
或者運行“tomcat6w //MS//”把它縮小到右下角。
啟動后,可以通過瀏覽器進行訪問,測試運行是否正常。
用IE或者Firefox等瀏覽器,輸入地址:
http://localhost:8080/ --如果安裝時修改了端口,請把8080用修改后的值替換。
如果能夠正常瀏覽到tomcat歡迎信息,就是正確了,否則就要檢查安裝是否正確、防火墻的設置等。
經常遇到的一個問題是端口沖突,最常見的就是80端口被占用,導致服務無法正常啟動。如果通過tomcat6 //TS//tomcat6來啟動,馬上就能夠發現提示信息。修改一下tomcat6\conf\server.xml中的端口就能夠解決。
1. IIS
2. skype即時聊天工具,skype的可以穿透防火墻的本領,也是通過占用80端口實現的。
進入windows命令行,輸入:
netstat -an
這個命令返回有4列:protocol協議、local address本機地址、foreign address來訪者地址、status狀態
瀏覽本機地址一列,可以看到當前主機對外服務的IP地址、端口都有哪些。
如果一臺機器有192.168.1.100和192.168.1.101兩個地址,那么:
0.0.0.0:80表示這臺機器上所有80端口都被使用
127.0.0.1:80表示127.0.0.1的80被使用,但.100和.101IP地址的80還未使用。
192.168.1.100:80表示.100IP地址的80端口被使用
192.168.1.101:80表示.101
想知道哪個程序使用了某個端口,可以通過天網防火墻或者是卡巴斯基獲得。
Tomcat.5.5.16
protected void deployApps() {
File appBase = appBase();
File configBase = configBase();
// Deploy XML descriptors from configBase
deployDescriptors(configBase, configBase.list());
// Deploy WARs, and loop if additional descriptors are found
deployWARs(appBase, appBase.list());
// Deploy expanded folders
deployDirectories(appBase, appBase.list());
}
從上面這段代碼,我們可以知道 tomcat
deployDescriptors(configBase, configBase.list());
File file = new File(System.getProperty("catalina.base"), "conf");
Container parent = host.getParent();
if ((parent != null) && (parent instanceof Engine)) {
file = new File(file, parent.getName());
}
file = new File(file, host.getName());
可以看出 configBase
System.getProperty("catalina.base") + “conf” + parent.getName()+host.getName()
parent.getName()
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" …
由此可以得出 configBase
(2)
第一步,首先獲取文件名(不包含 .xml
String nameTmp = files[i].substring(0, files[i].length() - 4);
String contextPath = "/" + nameTmp.replace('#', '/');
第二步,解析 xml
注意這里的 docBase
if (!docBase.isAbsolute()) {
docBase = new File(appBase(), context.getDocBase());
}
首先判斷 docBase
$CATALINA_HOME/webapps+docBase
// If external docBase, register .xml as redeploy first
if(!docBase.getCanonicalPath().startsWith(appBase().getAbsolutePath()))
{
//
$ CATALINA_HOME/webapps+docBase
isExternal = true;
...
if (docBase.getAbsolutePath().toLowerCase().endsWith(".war")) {
isWar = true;
}
} else{
// Ignore specified docBase
context.setDocBase(null);
}
從上面可以看出, <Context path="/xxxx" docBase="xxxx" .
deployWARs(appBase, appBase.list());
(1)
if (files[i].toLowerCase().endsWith(".war")) {
// Calculate the context path and make sure it is unique
String contextPath = "/" + files[i];
int period = contextPath.lastIndexOf(".");
(2)
return;
File xml = new File
(configBase, file.substring(0, file.lastIndexOf(".")) + ".xml");
if (deployXML && !xml.exists()) {
entry = jar.getJarEntry(Constants.ApplicationContextXml);
configBase.mkdirs();
。。。
先從 wart
$CATALINA_HOME/conf/Catalina/localhost/AAA.xml,
context.xml
( 4
deployDirectories(appBase, appBase.list());
(1)
File dir = new File(appBase, files[i]);
if (dir.isDirectory()) {
// Calculate the context path and make sure it is unique
String contextPath = "/" + files[i];
(2)
注意這里和上面的 war
TOMCAT部署項目有3種方法:1、直接把項目web文件夾放在webapps里2、修改conf里server.xml文件,添加一個Context,
<Context path="/projectName" reloadable="true" docBase="工程所在路徑\web" workdir="工程所在路徑\work"></Context>3、在Catalina/localhost目錄里,新增一個xml文件,添加一個Context內容,指向項目的目錄。<Context path="/目錄名" docBase="e:\example" debug="0" reloadable="true" />
優先級別為:3>2>1
第3個方法有個優點,可以定義別名。服務器端運行的項目名稱為path,外部訪問的URL則使用XML的文件名。這個方法很方便的隱藏了項目的名稱,對一些項目名稱被固定不能更換,但外部訪問時又想換個路徑,非常有效。
第2、3還有優點,可以定義一些個性配置,如數據源的配置等