Jetty是一个个开源的servlet容器,是使用java语言编写的,我们可以把Jetty容器实例化为一个对象,为一些独立的运(stand-alone)的java应用家里web和链接。
Jetty具有可扩展性,易用性,和易嵌入性。
以下,列出三种Jetty启动方式
- 在Server类中设置端口的方式
public static void startServer(){
try {
// 服务器的监听端口
Server server = new Server(8080);
// 关联一个已经存在的上下文
WebAppContext context = new WebAppContext();
// 设置描述符位置
context.setDescriptor("./WebContent/WEB-INF/web.xml");
// 设置Web内容上下文路径
context.setResourceBase("./WebContent");
// 设置上下文路径
context.setContextPath("/JettyDemo");
context.setParentLoaderPriority(true);
server.setHandler(context);
// 启动
server.start();
server.join();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
- 从xml配置文件中配置Jetty配置
public static void startServerByXml(){
Server server = new Server();
try {
XmlConfiguration config = new XmlConfiguration(new FileInputStream("./jetty/etc/jetty.xml"));
try {
config.configure(server);
// 给创建的server服务器配置处理的handler对象,可以通过xmlconfig配置,也可以手动配置,下面代码是手动配置完成的
ContextHandlerCollection handler = new ContextHandlerCollection();
// 创建一个webapp对象
WebAppContext webapp = new WebAppContext();
// 一定要有反斜杠,表示当前工程的上下文
webapp.setContextPath("/JettyDemo");
webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");
webapp.setResourceBase("./WebContent");
webapp.setDescriptor("./WebContent/WEB-INF/web.xml");
handler.addHandler(webapp);
server.setHandler(handler);
server.start();
server.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- 自定义启动Jetty
package com.dcfs.start;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jdk.internal.org.xml.sax.SAXException;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
public class StartServer extends Server {
// jetty.xml文件路径
private String xmlConfigPath;
private String contextPath;
// war包路径
private String warPath;
// webContent路径
private String resourceBase = "./web";
// web.xml文件路径
private String webXmlPath = "./web/WEB-INF/web.xml";
public StartServer(String xmlConfigPath, String contextPath,
String resourceBase, String webXmlPath) {
this(xmlConfigPath, contextPath, resourceBase, webXmlPath, null);
}
public StartServer(String xmlConfigPath, String contextPath) {
this(xmlConfigPath, contextPath, null, null, null);
}
public StartServer(String xmlConfigPath, String contextPath, String warPath) {
this(xmlConfigPath, contextPath, null, null, warPath);
}
public StartServer(String xmlConfigPath, String contextPath,
String resourceBase, String webXmlPath, String warPath) {
super();
if (StringUtils.isNotBlank(xmlConfigPath)) {
}
//判断是否是根据jetty的发布目录启动工程
if (StringUtils.isNotBlank(warPath)) {
this.warPath = warPath;
if (StringUtils.isNotBlank(contextPath)) {
this.contextPath = contextPath;
applyHandle(true);
}
} else{
if (StringUtils.isNotBlank(resourceBase))
this.resourceBase = resourceBase;
if (StringUtils.isNotBlank(webXmlPath))
this.webXmlPath = webXmlPath;
if (StringUtils.isNotBlank(contextPath)) {
this.contextPath = contextPath;
applyHandle(false);
}
}
}
public void startServer() {
try {
super.start();
System.out.println("current thread:"
+ super.getThreadPool().getThreads() + "| idle thread:"
+ super.getThreadPool().getIdleThreads());
super.join();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getXmlConfigPath() {
return xmlConfigPath;
}
public void setXmlConfigPath(String xmlConfigPath) {
this.xmlConfigPath = xmlConfigPath;
}
public String getContextPath() {
return contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
public String getWarPath() {
return warPath;
}
public void setWarPath(String warPath) {
this.warPath = warPath;
}
public String getResourceBase() {
return resourceBase;
}
public void setResourceBase(String resourceBase) {
this.resourceBase = resourceBase;
}
public String getWebXmlPath() {
return webXmlPath;
}
public void setWebXmlPath(String webXmlPath) {
this.webXmlPath = webXmlPath;
}
private void applyHandle(boolean warDeployFlag) {
ContextHandlerCollection handler = new ContextHandlerCollection();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath(contextPath);
webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");
if (!warDeployFlag) {
webapp.setResourceBase(resourceBase);
webapp.setDescriptor(webXmlPath);
} else {
webapp.setWar(warPath);
}
handler.addHandler(webapp);
super.setHandler(handler);
}
public void readXmlConfig() {
try {
XmlConfiguration config = new XmlConfiguration(new FileInputStream(
this.xmlConfigPath));
config.configure(this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注:将Jetty9的lib下jar包加入工程后,在访问jsp页面时,出现 con’t complie jsp class的错误,不知其原因。
相同的代码,将Jetty8的lib下的jar放入工程后一切正常