package jettyweb4;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.eclipse.jetty.server.NCSARequestLog;
import org.xml.sax.SAXException;
/**
* 总的服务类,本质上是一个分发器。使用jetty.xml配置。是通过继承server实现的。
* 还有一个总的日志记录器。
* @author Administrator
*
*/
public class JettyCustomServer extends Server {
private String xmlConfigPath;
private String contextPath;
private String warPath;
private String resourceBase = "src/main/webapp";
private String webXmlPath = "src/main/webapp/WEB-INF/web.xml";
private String webDefault = "jetty/contexts/webdefault.xml";
private String logPath = "target/jetty-yyyy_mm_dd.request.log";
private static final Log log = LogFactory.getLog(JettyCustomServer.class);
public JettyCustomServer(String xmlConfigPath, String contextPath,
String resourceBase, String webXmlPath, String warPath) {
super();
if (StringUtils.isNotBlank(xmlConfigPath)) {
this.xmlConfigPath = xmlConfigPath;
readXmlConfig();
}
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);
}
}
}
private void readXmlConfig() {
try {
XmlConfiguration configuration = new XmlConfiguration(JettyCustomServer.class
.getClassLoader().getResourceAsStream(this.xmlConfigPath));
configuration.configure(this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void applyHandle(Boolean warDeployFlag) {
ContextHandlerCollection handler = new ContextHandlerCollection();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath(contextPath);
webapp.setDefaultsDescriptor(webDefault);
if (!warDeployFlag) {
webapp.setResourceBase(resourceBase);
webapp.setDescriptor(webXmlPath);
} else {
webapp.setWar(warPath);
}
handler.addHandler(webapp);
//日志记录, 定义一个handlers集合;
HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection Context = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.setHandlers(new Handler[]{Context,new DefaultHandler(),requestLogHandler});
NCSARequestLog requestLog = new NCSARequestLog(logPath);
requestLog.setRetainDays(90);
requestLog.setAppend(true);
requestLog.setExtended(false);
requestLog.setLogTimeZone("GMT");
requestLogHandler.setRequestLog(requestLog);
//yaohc 集合中添加日志处理。
//getServer().setHandler(handlers);
handlers.addHandler(handler);
///////////
//yao_修改:传入一个handler集合,之前是传递一个handler
//super.setHandler(handler);
super.setHandler(handlers);
}
public void startServer() {
try {
log.debug("启动。。。。");
super.start();
System.out.println("current thread:"
+ super.getThreadPool().getThreads() + "| idle thread:"
+ super.getThreadPool().getIdleThreads());
super.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jetty嵌入Web编程多种实现方式案例
共47个文件
class:12个
java:12个
xml:10个
需积分: 10 12 下载量 181 浏览量
2016-12-14
18:48:39
上传
评论
收藏 39KB RAR 举报
温馨提示
jetty嵌入Web编程多种实现方式案例
资源推荐
资源详情
资源评论
收起资源包目录
jettyWebapp.rar (47个子文件)
jettyWebapp
.project 1KB
src
main
webapp
WEB-INF
web.xml 631B
index.jsp 367B
resources
jetty
etc
jetty.xml 4KB
contexts
webdefault.xml 24KB
jetty.properties 189B
java
jettyweb4
Ap.java 263B
JettyCustomServer.java 5KB
jettyweb3
OneServletContext.java 857B
SimpleServerStart.java 1011B
HelloWorld.java 1KB
jettyweb1
App.java 409B
TestHandler.java 709B
test
Test.java 563B
jettyweb5
file.xml 945B
FileServer.java 1KB
FileServerWithXml.java 576B
jettyweb2
MyServlet.java 2KB
App2.java 2KB
target
m2e-jee
web-resources
META-INF
MANIFEST.MF 114B
maven
cn.com.songjy
jettydemo
pom.properties 231B
pom.xml 3KB
classes
jettyweb4
Ap.class 661B
JettyCustomServer.class 5KB
jettyweb3
OneServletContext.class 1KB
HelloWorld.class 2KB
SimpleServerStart.class 1KB
jettyweb1
TestHandler.class 1KB
App.class 977B
jetty
etc
jetty.xml 4KB
contexts
webdefault.xml 24KB
test
Test.class 1KB
jetty.properties 189B
jettyweb5
FileServer.class 1KB
file.xml 945B
FileServerWithXml.class 1KB
jettyweb2
App2.class 3KB
MyServlet.class 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 296B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 664B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 674B
.jsdtscope 530B
pom.xml 3KB
.classpath 1KB
共 47 条
- 1
资源评论
yao_2008
- 粉丝: 49
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- kernel-ml-6.8.8-1.el7.elrepo.x86-64.rpm
- Labview基本框架之状态机
- HM2309B-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- Git安全实践:保护你的代码仓库个人学习笔记.md
- 自动驾驶定位系列教程九:后端优化.pdf
- 三国志5威力加强版-windows
- HM2309A-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2306-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- Git进阶技巧:提升团队协作效率个人学习笔记.md
- 自动驾驶定位系列教程八:建图系统结构优化.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功