- Eclipse 和 Tomcat 都直接下載後解壓縮即可執行,不需安裝。
- 從 Server View 中新增 Server,選擇 Apache --> Tomcat Vx.x
- 重要!第三步很容易忽略掉而使得編譯完後執行產生 404 error。
在 Server view 中對剛剛新增的 Server 點兩下,在 General Information --> Server location 中,選擇 Use Tomcat Installation (takes control of Tomcat installation)
這個步驟在剛新增完 Server 就要立刻做,事後這個選單會被 disabled 無法改變!
原因 (摘錄自此連結):
Re: problem loading localhost:8080 in eclipse
This is normal. To see why, double-click on the Tomcat server in the Servers view. This will open the Tomcat configuration editor. Click on the "Open launch configuration" link in the Overview section. This will open the launch configuration properties dialog. Select the Arguments tab and examine the contents of the VM Arguments field.
Note that the catalina.home property points to your Tomcat installation, but catalina.base points to a ".metadata\.plugins\org.eclipse.wst.server.core\tmp?" directory under your workspace. Thus, you are running a separate instance of Tomcat. The "webapps" directory under the ".metadata\...\tmp?" directory contains only an "empty" ROOT webapp plus any web projects you have added to the server. This is why you get the 404.
In the Tomcat configuration editor, you can uncheck the "Run modules directly from the workspace (do not modify the Tomcat installation)" option and catalina.base and catalina.home will both be set to your Tomcat installation. Be aware that in this configuration, the Tomcat server in Eclipse "owns" your Tomcat installation. Every time you start the Tomcat server from Eclipse, the Tomcat files under the Servers project in your workspace will overwrite the files in your installation.
It was assumed the most would want to keep their Tomcat installation independent from Eclipse Tomcat server, so the default is to create a separate Tomcat instance. With separate instances, you can run the Tomcat installation at the same time as the Eclipse Tomcat server provided you modify one or both of them so that the ports they use do not conflict.
If you would like the standard Tomcat webapps present while keeping the separate Tomcat instance in Eclipse, switch to the Modules tab in the Tomcat configuration editor and use the "Add External Web Module" button to manually add the desired webapps. Note that this will add a little bit to the startup time for the server. - 新增 Project,類型選擇 Web --> Dynamic Web Project
- 在 Project View 對剛剛新增的 project 點右鍵 new --> JSP,取名為 index.jsp
- 把以下內容貼到 index.jsp中。
- 在 Server View 中,對該 Server 點右鍵,選擇 publish (or republish)
- 回到 Project View, 選 Run --> Run as --> Run on Server
<% java.util.Date d = new java.util.Date(); %>
<h1>
Today's date is <%= d.toString() %> and this jsp page worked!
</h1>
0 意見:
張貼意見