参考官网,jetty嵌入web项目
public class Launcher {
public static final int DEFAULT_PORT = 8080;
public static final String DEFAULT_CONTEXT_PATH = "../CSM-APP/web";
public static void runJettyServer(int port, String contextPath) {
Server server = createJettyServer(port, contextPath);
// Start things up!
// The use of server.join() the will make the current thread join and
// wait until the server is done executing.
// See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Server createJettyServer(int port, String contextPath) {
Server server = new Server(port);
server.setStopAtShutdown(true);
// Setup JMX
MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean( mbContainer );
WebAppContext webapp = new WebAppContext();
webapp.setContextPath( "/" );
File warFile = new File(contextPath+"/src/main/webapp");
webapp.setWar(warFile.getAbsolutePath());
webapp.addAliasCheck(new AllowSymLinkAliasChecker());
// webapp.setAttribute(
// "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
// ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$" );
server.setHandler(webapp);
return server;
}
}