java web專案中整合Jetty作為web容器
阿新 • • 發佈:2019-01-09
1、準備好一個非常簡單點的web專案(maven專案)
2、準備好maven環境,並配置pom檔案,關於jetty內容如下:
3、使用eclipse對maven專案進行build,獲取build後的專案目錄(或者將專案達成war包)
4、建立執行配置jetty的Server類
執行war包的類
執行build後的專案目錄
然後執行專案,輸入http://localhost:8080/myapp即可訪問專案首頁
<!-- jetty dependecies begin --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.1.4.v20140401</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.1.4.v20140401</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-continuation</artifactId> <version>9.1.4.v20140401</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>9.1.4.v20140401</version> </dependency> <!-- jetty dependecies end -->
public class WebAppWarServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setContextPath("/myapp"); context.setWar("E:/share/test/xxx.war"); server.setHandler(context); server.start(); server.join(); } }
public class WebAppContextWithFolderServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setContextPath("/myapp"); context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml"); context.setResourceBase("E:/share/test/struts2-blank"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); server.join(); } }