1、Eclipse建立Tomcat服務
1.1 新建Server
首先這裏是指,jee版的Eclipse。Eclipse是沒有像MyEclipse那樣集成Tomcat的,需要我們自己設置。
New -> Other -> Server ,然後選擇Apache下的tomcat的版本。
注意:如果Next或Finish按鈕都是灰的,那麼需要到Windw- > Preferences -> Server -> Runtime Environments 中進行設置。需要增加愛一個tomcat運行環境。指定tomcat的路徑和jre。如圖:
1.2設置Tomcat Server
接着新建Server說,點擊Next後,會讓添加項目,先不要添加,還需要配置一些地方。Finish。
打開Servers窗口,裏面會有我們剛建好的Tomcat,雙擊它就會打開配置文件。
設置1:在Server Locations裏,選擇Use Tomcat installation。就是發佈項目時把文件發佈到Tomcat安裝目錄下。默認是在Eclipse的Server項目中,路徑特別深,所以想查看文件特別麻煩。
設置2:在Server Options裏,選擇Publish module contexts to separate XML files。如果不選一般會報錯。
did not find a matching property。
what?: 將模塊上下文發佈到單獨的XML文件中
這一步的主要目的在於 :
- 使本來配置在conf/server.xml裏的元素,separate到/conf/Catalina/localhost/的目錄下專門生成一個{context_name}.xml。
設置3:如果項目比較大,可能啓動時間較長,但是tomcat設置了超時時間,這個時候我們就需要把超時時間改長一些。在Timeouts更改。
直接修改web應用的Tomcat的Timeouts配置,如下圖,將Start的45修改爲更大的數值
設置4:在ports可以更改端口號。
1.3錯誤:The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
實際這是建議使用apache的apr。如果想使用apr的話, 實際操作很簡單,下載 http://tomcat.heanet.ie/native/1.1.1/binaries/win32/tcnative-1.dll。將這個文件複製到C:\WINDOWS\system32\下面然後重新啓動tomcat。
2,添加刪除發佈項目
這個就簡單了,先打開Server窗口,右擊Tomcat。
Add and remove 添加刪除項目。不是Dynamic Web類型的項目將不允許添加。
Publish 發佈項目。
Start 啓動tomcat。
3、Server Options設置
默認的配置如下:
這個默認配置存在一個問題,每次保存JSP時tomcat會自動重啓,this is quite annoying..解決辦法是
Ctrl+Shift+R,找到server.xml
將reloadable="true"改爲false,這樣保存JSP時Tomcat不會自動重啓
接下來看看圖中幾個參數的含義:
(1)serve modules without publishing:
讓tomcat直接使用WebContent下的東西
省去publish到<workspace>\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
勾上它會將tmp0\wtpwebapps下相關的resources刪除,
同時會修改Servers/Tomcat v6.0 Server at localhost-config/servers.xml中的serveModulesWithoutPublish=true
以及tmp0/conf/server.xml中的<Context>配置
(2)publish module contexts to separate XML files
勾選 上後會修改Servers/Tomcat v6.0 Server at localhost-config/servers.xml中的saveSeparateContextFiles=true
同時將tmp0/conf/server.xml中關於該app的<Context>元素
移動到tmp0/conf/Catalina/localhost/appName.xml中..
修改JSP後立即生效
在Servers視圖中雙擊tomcat
勾選上Automatically publish when resources change
這樣修改JSP後tomcat能熱加載。。(這個選項對java文件無效,關了以後每次也能熱加載)
參考:http://stackoverflow.com/questions/921684/how-to-tell-eclipse-3-4-wtp-to-not-restart-tomcat-on-jsp-save
OSC中能找到的:http://www.oschina.net/question/17793_29961
4、Server Locations 設置
Eclipse中tomcat service設置
選擇window ----show view---services可以看到服務的面板
雙擊tomcat進入配置界面Service Locations(Specify the server path (i.e. catalina.base) and deploy path. Server must be published with no modules present to make changes.)選項變灰色無法更改配置。
若要更改,則先把tomcat下的所有項目移除。並右擊,clean...之後方可設置。。。啓動後將又變爲黑色。
默認選項爲:
Use workspace metadata(dose not modify Tomcat installation)
修改選項爲:
Use Tomcat installation(takes control of Tomcat installation)
這樣在Eclipse啓動了tomcat服務器,我們也能夠訪問到tomcat本地管理首頁。
否則只能在DOS下使用Tomcat的啓動命令才能訪問Tomcat本地管理首頁。