第一种,直接在maven pom文件中引用
<dependencies> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all-server</artifactId> <version>7.0.2.v20100331</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jsp-2.1-jetty</artifactId> <version>7.0.0.pre5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> </dependencies>
public class StartJetty {
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(8080);
server.setConnectors(new Connector[] { connector });
WebAppContext webAppContext = new WebAppContext("webapp", "/webs");
// webAppContext.setContextPath("/");
String path = "src/main/";
webAppContext.setDescriptor(path+"webapp/WEB-INF/web.xml");
webAppContext.setResourceBase(path+"webapp");
webAppContext.setDisplayName("webs");
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 {
server.start();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("server is start");
}
}
第二种方式
直接安装 run-jetty-run