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的更多相关文章

  1. eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法

    eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...

  2. eclipse项目中启动项目无法载入类

    在eclipse 项目中,当载入jar包后,加载里面的包,可以找到此类,但是编译运行的时候报错java.lang.ClassNotFoundException: 1,路径名未写正确: 2,配置出错; ...

  3. WebCollector2.7爬虫框架——在Eclipse项目中配置

    WebCollector2.7爬虫框架——在Eclipse项目中配置 在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可. Netbea ...

  4. 关于Eclipse项目中加入jquery.js文件报错(missing semicolon)问题

    在使用Eclipse3.7及以后的版本的时候,加入jQuery文件会报错(missing semicolon),文件中会显示红色小X,虽然这个错误并不会影响项目的运行,但是这个却会大大的影响到开发人员 ...

  5. Eclipse项目中web app libraries和 Referenced Libraries区别

    Referenced  Libraries是编译环境下使用的JAR包,所谓编译环境下使用的JAR包, 就是说你在Eclipse中进行源文件的编写的时候,所需要引用到的类都从Referenced  Li ...

  6. (转) eclipse项目中.classpath文件详解

    背景:对于java项目中.classpath文件中的相关定义一直不是很了解,有必要进行深入的学习. 1 前言 在使用eclipse或者myeclipse进行Java项目开发的时候,每个project( ...

  7. eclipse项目中.classpath文件详解

    1 前言 在使用eclipse或者myeclipse进行java项目开发的时候,每个project(工程)下面都会有一个.classpath文件,那么这个文件究竟有什么作用? 2 作用 .classp ...

  8. eclipse项目中引入shiro-freemarker-tags会jar包冲突

    maven项目中引入了这个依赖. <dependency> <groupId>net.mingsoft</groupId> <artifactId>sh ...

  9. eclipse项目中丢失的R包找回方法

    当我们项目中的R文件丢失的时候会令我们痛苦不已,怎样找回呢?总不能删了吧,那样心血会毁于一旦的,我们肯定不会那样做,那要怎么办呢?我这里提供三种方法: ​一,一般情况下这样: ​    ​方法一:选中 ...

随机推荐

  1. [数据算法]D1.BloomFilter

    BloomFilter是一种高效的去重算法,算法的要义是散列对比. 1.原理 当一个元素加入集合时,判断这个元素是否 2.举例 例如我要对URL去重(这个在爬虫上可以用): URL1 -> 3. ...

  2. cef-3.2623 build on vs2013

    1. 参文"在Windows下编译Cef3.2623并加入mp3.mp4支持(附带源码包和最终DLL)"下载包 http://blog.csdn.net/zhuhongshu/ar ...

  3. .NET+MySql 踩坑1

    换成MySql数据库后,遇到的问题: 已解决,但不理解的问题: var test = db.test; 报如下图错误: 加上DefaultIfEmpty()则解决. var test = db.Tes ...

  4. python基本概念

    python环境以及python的搭建的基本知识 python解释器 python语言的本质 通过解释器将脚本翻译成机器能识别的二进制码,交予机器执行 pycharm ide:集成开发环境 集成编译器 ...

  5. angular6 http.service.ts

    import { Injectable, isDevMode } from '@angular/core'; import { HttpClient, HttpParams, HttpHeaders ...

  6. ckeditor自定义插件--一键给所有的图片添加链接

    ckeditor自定义插件在网上查了查,感觉还是比较好用的,写了一个一键给编辑器中的所有图片添加链接. 在ckeditor目录下的plugins下建以插件为名的文件夹,在里边建plugin.js文件, ...

  7. ajax入门学习

    1.   XMLHttpRequest 是 AJAX 的基础. 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). XMLHttpReq ...

  8. python 变量之小整数池跟大整数池

    在python中定义变量会有:id,type,value.对于==比较的是value,对于is比较的是id. 因此,对于相同value的变量,它的type相同,但是它的id值可能不一样.对于相同id的 ...

  9. Tensorflow(添加噪声的方式)

    在去噪自编码器中,模型的输入是原始的输入经过某种形式的加噪过程后的衰弱的形式,所以加噪声一般分为:加高斯白噪声,掩模噪声,椒盐噪声. 1.加性高斯噪声 self.scale = tf,placehol ...

  10. 说说Android项目中的armeabi,armeabi-v7a和x86

    1.区别    这三者都表示的是CPU类型,早期的Android系统几乎只支持ARMv5的CPU架构,但是现在已经有7种了.ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MI ...