對比 MyEclipse 等重量級插件,Eclipse 原生整合 Jetty 只需幾步配置,即可獲得極速啟動、零干擾的 Web 應用調試體驗。適合 Maven 項目、Spring MVC、純 Servlet/JSP 等場景。本文記錄標準配置流程。
D:/jetty-9.4.x)myweb/
├─ src/main/java/ # Java 源碼
├─ src/main/webapp/ # Web 資源 (JSP, CSS, JS, WEB-INF/web.xml)
│ └─ WEB-INF/
│ ├─ web.xml
│ └─ lib/ # 無需手動放 JAR,Maven 編譯輸出到 target/classes,Eclipse 自動加入 Classpath
└─ pom.xml

在 pom.xml <dependencies> 添加 (按需選版本,示例 Jetty 9 + JSP 2.3):
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.53.v20231009</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.53.v20231009</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.4.53.v20231009</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
原文截圖展示手動加 JAR 方式,Maven 管理更優雅,此處補充最佳實踐。

Jetty-WebApp。org.eclipse.jetty.xml.XmlConfiguration (Jetty 9+ 啟動入口)etc/jetty.xml (或自定義配置 etc/jetty-myweb.xml)D:/jetty-9.4.x),相對路徑 etc/ 才能解析正確。target/classes、Maven 依賴庫、Jetty 所有 JAR (Maven 自動管理無需手動加)。

在 Jetty 目錄 etc/jetty-myweb.xml:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/myweb</Set>
<Set name="war"><SystemProperty name="jetty.home"/>../myweb/target/myweb-1.0.0-SNAPSHOT</Set> <!-- 指向 Maven 輸出的爆炸目錄或 WAR -->
<Set name="extractWAR">false</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home"/>/etc/webdefault.xml</Set>
</Configure>
關鍵點:war 指向項目編譯輸出目錄 (Maven target/<artifactId>-<version>),修改 JSP/Java 後 無需重啟 Jetty,Eclipse 增量編譯自動生效 (JSP 修改即時生效,Java 類需重啟或配合 HotSwap/JRebel)。
Started ServerConnector@...{HTTP/1.1}{0.0.0.0:8080} 即成功。https://localhost:8080/myweb/ 測試功能。contextPath 錯開端口復用。Run → Debug Configurations 同樣設置,斷點調試零差別。mvn jetty:run 更簡單,但無法在 Eclipse Debug 視圖直觀斷點,視團隊習慣選擇。Eclipse + Jetty = 輕量、快速、標準。核心三要素:Main Class = XmlConfiguration、Working Directory = Jetty Home、Program Arguments = etc/jetty-xxx.xml。拒絕臃腫插件,擁抱原生調試效率。