首先,在pom.xml加入jettyjar包以及支持jspjar包:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>7.2.2.v20101205</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
</dependency>
創建一個java類:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class GameAdmin {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Server server = buildNormalServer(8080, "/gameweb");
server.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 創建用于正常運行調試的Jetty Server, 以src/main/webapp為Web應用目錄.
*/
public static Server buildNormalServer(int port, String contextPath) {
Server server = new Server(port);
WebAppContext webContext = new WebAppContext("src/main/webapp", contextPath);
webContext.setClassLoader(Thread.currentThread().getContextClassLoader());
server.setHandler(webContext);
server.setStopAtShutdown(true);
return server;
}
}