首先jetty是啥呢?Jetty 是一個開源的servlet容器,它爲基於Java的web容器,例如JSP和servlet提供運行環境。Jetty是使用Java語言編寫的,它的API以一組JAR包的形式發佈。開發人員能夠將Jetty容器實例化成一個對象,能夠迅速爲一些獨立運行(stand-alone)的Java應用提供網絡和web鏈接。——百度百科java
說白了,jetty能夠像Tomcat同樣讓運行web應用。web
有什麼好處呢?jetty能夠很方便的設置項目名和訪問端口,在你須要啓動多個項目的時候就很方便了。網絡
怎麼使用呢?接下來講說怎麼在項目中嵌入jettyapp
首先是pom文件中須要添加:eclipse
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.2.10.v20150310</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.2.10.v20150310</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
其次是jetty的啓動類:webapp
/** * *@description *@auther panmingshuai *@time 2018年4月7日下午3:41:18 * */ public class JettyServer { private static final Logger LOGGER = Logger.getLogger(JettyServer.class); private int port; private String contextPath; private String webappPath; private Server server; private WebAppContext webapp; /** * 建立用於開發運行調試的Jetty Server, 以src/main/webapp爲Web應用目錄. */ public static void main(String[] args) { JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81); jettyServer.start(); } public JettyServer(String contextPath, String webappPath, int port) { if (StringUtils.isBlank(contextPath)) { this.contextPath = ""; } else { this.contextPath = contextPath; } if (StringUtils.isBlank(webappPath)) { throw new IllegalArgumentException("webappPath is required"); } else { this.webappPath = webappPath; } this.port = port; } public void start() { if (null == server || server.isStopped()) { doStart(); } else { throw new IllegalStateException("JettyServer already started."); } } private void doStart() { if (!checkServerPortAvailable()) { throw new IllegalStateException("Server port already in use: " + port); } server = new Server(); // 設置在JVM退出時關閉Jetty的鉤子。 server.setStopAtShutdown(true); // 這是http的鏈接器 ServerConnector connector = new ServerConnector(server); connector.setPort(port); // 解決Windows下重複啓動Jetty竟然不報告端口衝突的問題. connector.setReuseAddress(false); server.setConnectors(new Connector[] { connector }); webapp = new WebAppContext(webappPath, contextPath); server.setHandler(webapp); try { long st = System.currentTimeMillis(); server.start(); long sp = System.currentTimeMillis() - st; System.out.println("JettyServer " + Jetty.VERSION + " started: " + String.format("%.2f sec", sp / 1000D) + ",the port is ===" + port + ""); server.join(); } catch (Exception e) { e.printStackTrace(); LOGGER.error("JettyServer started failed!"); } } private boolean checkServerPortAvailable() { if (0 < port) { ServerSocket ss = null; try { ss = new ServerSocket(port, 0, null); } catch (Exception e) { LOGGER.error("check serverPort failed", e); return false; } finally { if (null != ss) { try { ss.close(); } catch (IOException e) { LOGGER.error("close ServerSocket failed", e); } } } } else { throw new IllegalArgumentException("Invalid port " + port); } return true; } }
須要注意的是:JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81);這個方法中ps是你訪問的項目名,81是訪問端口,即你要訪問項目的地址是:localhost:81/psui