在開發Java web項目時候,可以在項目中嵌入Jetty服務的方式來運行web程序。
由於最近開發web項目,自己使用的是比較舊的eclipse不支持導入tomcat來運行項目,於是就學習了下使用項目中Jetty來運行項目。
-
采用Jetty Plugin
在pom文件中引入Jetty Plugin配置即可:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties><build>
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.15.v20140411</version> <configuration> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/web</contextPath> //你自己的工程名稱 </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8080</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <contextHandlers> <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext"> <resourceBase>${project.parent.basedir}/src/main/webapp</resourceBase> <contextPath>/web</contextPath> </contextHandler> </contextHandlers> </configuration> </plugin> </plugins> </build>
運行時候只要maven build->輸入jetty:run即可。
這種插件方式運行的項目不支持@ServerEndpoint websocket功能,不知道是不是我這種方式使用問題還是什么,知道的指導下。我下面自制JettyServer就可以支持websocket,目前我開發的時候采用的方式。
-
自制Jetty服務類
這種方式可以支持websocket,如果項目中需要使用到可以試試這種。
首先pom.xml引入jetty的依賴:
<dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all</artifactId> <version>9.2.14.v20151106</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>9.2.15.v20160210</version> </dependency>
接下來自己寫一個JettyServer類:我自己的完整代碼如下
import javax.websocket.server.ServerContainer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; import org.json.JSONObject; import com.web.test.MyWebSocket; public class JettyServer { public static void main(String[] args) { int port = 8080; Server server = new Server(port); WebAppContext webAppContext = new WebAppContext("webapp","/web"); webAppContext.setDescriptor("webapp/WEB-INF/web.xml"); webAppContext.setResourceBase("src/main/webapp"); webAppContext.setDisplayName("web"); webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader()); webAppContext.setConfigurationDiscovered(true); webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext); System.out.println(webAppContext.getContextPath()); System.out.println(webAppContext.getDescriptor()); System.out.println(webAppContext.getResourceBase()); System.out.println(webAppContext.getBaseResource()); try { ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(webAppContext); // Add WebSocket endpoint to javax.websocket layer wscontainer.addEndpoint(MyWebSocket.class); //這行是如果需要使用websocket就加上,不需要就注釋掉這行,mywebsocket是自己寫的websocket服務類 server.start(); } catch (Exception e) { e.printStackTrace(); } System.out.println("server is start, port is "+port+"............"); } }
運行項目就只要運行這個main函數即可。
假如正式發布需要放到tomcat里運行,需要把下面這個依賴去掉,tomcat和下面的依賴不兼容,會報錯(javax.servlet.ServletException: Not running on Jetty, JSR-356 support unavailable)
這種方式運行項目可以在開發的時候用用
-
<dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all</artifactId> <version>9.2.14.v20151106</version> </dependency>