eclipse 项目中嵌入jetty
Jetty是一个提供HHTP服务器、HTTP客户端和javax.servlet容器的开源项目,Jetty 目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器,它有一个基本数据模型,这个数据模型就是 Handler,所有可以被扩展的组件都可以作为一个 Handler,添加到 Server 中,Jetty 就是帮你管理这些 Handler。
最近工作中需要在项目中集成jetty,由于之前从来没有用过jeety,所以耗费了我多半天的时间去学习,基本实现了jetty嵌入集成,我自己搭建了一个简单的springMvc框架,简单实现了controller请求跳转jsp页面的小功能,这里springMvc的创建就不在这里叙述了。不会的可以去网上上查找资料。项目结构如下:

首先在项目中导入相应的jar包,如下:
jetty-all-8.1.17.v20150415.jar
servlet-api.jar
ant-1.6.5.jar
core-3.1.1.jar
jsp-2.1.jar
jsp-api-2.1.jar
ant-xmltask.jar
jetty-util-6.1.9.jar
其次创建一个入口方法用于启动 jetty,代码如下:
/**
* 以内置Jetty模式启动Web应用
* 提示:请以debug as java application的方式启动
*/
public class AOS { /**
* 启动内置服务器
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
AOSServer aosServer = new AOSServer();
aosServer.setWebContext("/testjetty"); //项目启动的上下文名称
aosServer.setPort(10010); //服务的端口号
aosServer.start(); //启动服务
}
}
最后编写具体的jetty启动方法,代码如下:
/**
* <b>基于Jetty的嵌入式Servlet容器</b>
*
* @author atom.wu
* @date 2008-06-06
* @since 1.0
*/
public class AOSServer { private static Logger log = LoggerFactory.getLogger(AOSServer.class); /**
* 监听端口, 缺省为80
*/
private int port = 80; /**
* 应用上下文, 缺省为/(无上下文)
*/
private String webContext = "/"; public AOSServer() { }
/**
* 构造Server实例
*
* @param pWebContext
* @param pPort
*/
public AOSServer(String pWebContext, int pPort) {
setWebContext(pWebContext);
setPort(pPort);
} public int getPort() {
return port;
} /**
* 监听端口, 缺省为80
*
* @param port
*/
public void setPort(int port) {
this.port = port;
} public String getWebContext() {
return webContext;
} /**
* 应用上下文, 缺省为/(无上下文)
*
* @param webContext
*/
public void setWebContext(String webContext) {
this.webContext = webContext;
} /**
* 启动Jetty容器
*/
public void start() throws Exception {
long start = System.currentTimeMillis();
final String webRoot = System.getProperty("user.dir") + "/WebContent"; //工程路径
Server server = new Server();
// 设置在JVM退出时关闭Jetty的钩子。
server.setStopAtShutdown(true);
SelectChannelConnector connector = new SelectChannelConnector();
//disable nio cache to unlock the css and js file when running
connector.setUseDirectBuffers(false);
// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.
connector.setReuseAddress(false);
connector.setPort(port);
server.setConnectors(new Connector[]{connector});
WebAppContext context = new WebAppContext();
context.setResourceBase("WebContent");
context.setContextPath(webContext);
//设置表单提交大小 (缺省值:200000)
context.setMaxFormContentSize(10000000);
context.setParentLoaderPriority(true);
//针对jetty使用jstl的特殊设置,扫描tld文件。指定哪些jar中可能含有tld。
context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/.*jsp-api-[^/]*\\.jar$|.*/.*jsp-[^/]*\\.jar$|.*/.*taglibs[^/]*\\.jar$|.*/.*jstl[^/]*\\.jar$");
server.setHandler(context); boolean isSuccess = true;
try {
server.start();
} catch (BindException e) {
isSuccess = false;
} catch (Exception e) {
isSuccess = false;
} finally{
String msg = "sa-web启动成功";
String supportMsg = " ";
if ( !isSuccess) {
msg = "sa-web启动失败";
log.error(msg);
msg = msg + supportMsg;
System.out.println(msg);
System.exit(0);
}else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
webContext = webContext.equals("/") ? "" : webContext;
String webUrl = "http://localhost";
if (port == 80) {
webUrl = webUrl + webContext;
}else {
webUrl = webUrl + ":" + port + webContext;
}
StringSelection stringSel = new StringSelection(webUrl);
clipboard.setContents(stringSel, null);
long alltime = System.currentTimeMillis() - start;
msg = msg + "[" + alltime + "毫秒]" + " >> " + webUrl + supportMsg;
System.out.println(msg);
server.join(); //线程阻塞
}
}
}
启动jetty 服务。

浏览器打开地址:

eclipse 项目中嵌入jetty的更多相关文章
- eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法
eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...
- eclipse项目中启动项目无法载入类
在eclipse 项目中,当载入jar包后,加载里面的包,可以找到此类,但是编译运行的时候报错java.lang.ClassNotFoundException: 1,路径名未写正确: 2,配置出错; ...
- WebCollector2.7爬虫框架——在Eclipse项目中配置
WebCollector2.7爬虫框架——在Eclipse项目中配置 在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可. Netbea ...
- 关于Eclipse项目中加入jquery.js文件报错(missing semicolon)问题
在使用Eclipse3.7及以后的版本的时候,加入jQuery文件会报错(missing semicolon),文件中会显示红色小X,虽然这个错误并不会影响项目的运行,但是这个却会大大的影响到开发人员 ...
- Eclipse项目中web app libraries和 Referenced Libraries区别
Referenced Libraries是编译环境下使用的JAR包,所谓编译环境下使用的JAR包, 就是说你在Eclipse中进行源文件的编写的时候,所需要引用到的类都从Referenced Li ...
- (转) eclipse项目中.classpath文件详解
背景:对于java项目中.classpath文件中的相关定义一直不是很了解,有必要进行深入的学习. 1 前言 在使用eclipse或者myeclipse进行Java项目开发的时候,每个project( ...
- eclipse项目中.classpath文件详解
1 前言 在使用eclipse或者myeclipse进行java项目开发的时候,每个project(工程)下面都会有一个.classpath文件,那么这个文件究竟有什么作用? 2 作用 .classp ...
- eclipse项目中引入shiro-freemarker-tags会jar包冲突
maven项目中引入了这个依赖. <dependency> <groupId>net.mingsoft</groupId> <artifactId>sh ...
- eclipse项目中丢失的R包找回方法
当我们项目中的R文件丢失的时候会令我们痛苦不已,怎样找回呢?总不能删了吧,那样心血会毁于一旦的,我们肯定不会那样做,那要怎么办呢?我这里提供三种方法: 一,一般情况下这样: 方法一:选中 ...
随机推荐
- linux 每天一点点 【ubuntu16.04 】
1.查看防火墙状态 sudo ufw status [非root用户需要sudo] inactive状态是防火墙关闭状态 active是开启状态 2.查看CentOS版本信息 cat /etc ...
- XAMPP启动Apache时发生ERROR
XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包,是一个易于安装且包含 MySQL.PHP 和 Perl 的 Apache 发行版.XAMPP 的确非常容易安装和 ...
- 正试图在os加载程序锁内执行托管代码。不要尝试在DllMain或映像初始化函数内运行托管代码 问题解决方法
方法一把vs菜单的 调试->异常->Managed Debuggin Assistants->LoaderLock
- rtmp详解
文件下载地址: 中文:https://files.cnblogs.com/files/bugutian/rtmp_specification_1.0_cn.zip 英文:http://www.adob ...
- BZOJ2662 [BeiJing wc2012]冻结
网上的题解都是分层图+spfa或者dijkstra 我觉得dijk太难写了,懒得写,看了一下数据范围$N=50$,这显然是出题人勾引人犯罪 我决定使用floyd的做法,令$f[i][j][t](k)$ ...
- 如何不使用loop循环创建连续的数组
ES5 Array.apply(null,{length:100}) Object.keys(Array.apply(null,{length:100})); ES6 [...Array(100)]
- 微信或QQ屏蔽域名,爆红域名如何在微信打开,如何进行微信域名防封?
近很多朋友都会遇到这个问题,为什么我的微信域名或者QQ域名怎么总是提示拦截呢?在这里跟大家说一下吧: 第一点:就是域名里面的内容违规或者诱导被举报而导致的拦截 第二点:就是被用户或者同行恶意举报而导致 ...
- 怎样用div做三角形
20181204 用盒子模型做三角形的效果: <div></div> width:0px; height:0px; border:10px solid red; border- ...
- rabbitmq 日志存储路径
Linux 下/var /log/rabbitmq/ windows下C:\Users\Administrator\AppData\Roaming\RabbitMQ\log
- 基于服务器AAA的实验
1.实验拓扑 2.地址分配 Device Interface IP Address Subnet Mask R0 Fa0/0 192.168.1.2 255.255.255.0 S ...