jetty的启动有三种方式,我这里用main方法直接启动的方式
要解决两个问题:
1.导包要全
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>8.2.0.v20160908</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.2.0.v20160908</version>
</dependency>
导入上面的两包即可,还看到有说这个包跟tomcat的包有冲突,要是用tomcat启动最好包这些包注释了。
2.路径问题
package com.iflytek.start;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args) {
// 服务器的监听端口
Server server = new Server(9999);
// 关联一个已经存在的上下文
WebAppContext context = new WebAppContext();
// 设置描述符位置
context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");
// 设置Web内容上下文路径
context.setResourceBase("./src/main/webapp");
// 设置上下文路径
context.setContextPath("/JettyDemo");
context.setParentLoaderPriority(true);
server.setHandler(context);
try {
server.start();
// server.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("server is start");
}
}
最后附上请求路径:http://localhost:9999/JettyDemo/index.jsp