|
一、80端口启动
package com.tools;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class JettyServer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Server server = buildNormalServer(8080, "/");
server.start();
}
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.setAttribute("org.eclipse.jetty.Request.maxFormContentSize", 10000000);
server.setStopAtShutdown(true);
return server;
}
}
二、443端口启动
public static void main(String[] args) throws Exception {
Server server = buildNormalServer(443, "/");
server.start();
}
/**
* 创建用于正常运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
*/
public static Server buildNormalServer(int port, String contextPath) {
System.setProperty("org.eclipse.jetty.util.log.class", StdErrLog.class.getName());
// 设置Jetty日志
Server server = new Server();
// 设置ssl连接器
SslSocketConnector ssl_connector = new SslSocketConnector();
ssl_connector.setPort(port);
SslContextFactory cf = ssl_connector.getSslContextFactory();
cf.setKeyStorePath("passport.keystore");
cf.setKeyStorePassword("123456");
cf.setKeyManagerPassword("123456");
server.addConnector(ssl_connector);
// 设置context
WebAppContext context = new WebAppContext();
context.setResourceBase("./src/main/webapp");
context.setContextPath(contextPath);
// context.setDefaultsDescriptor("src/test/java/jetty/webdefault.xml");
// PS:嵌入式的Jetty,应用当前工程的ClassPath,如果不设置将使用WebAppClassLoder,WEB-INF/lib目录加载jar。
context.setClassLoader(Thread.currentThread().getContextClassLoader());
context.setParentLoaderPriority(true);
server.setHandler(context);
return server;
} |
|
|