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. jmeter系列-如何实现像loadrunner一样,多个并发用户先通过登录初始化,然后做并发的接口性能压测

    自动转开发后,就很少关注性能测试方面的东西,最近在帮朋友做一个性能压测,由于朋友那边的公司比较小,环境比较简单,而且是对http服务进行的压测,所以最终 选用了jmeter来实现这个压测. 如下就是我 ...

  2. python 学习笔记 4 ----> dive into python 3

    解析 列表解析.字典解析.集合解析 浏览本地文件系统的模块: 1 os 2 os.path 3 glob os模块:获取(和修改)本地目录.文件进程.环境变量等信息 os.path模块:包含了操作路径 ...

  3. 瀑布流布局(等宽不等高jQuery)

    在百度上看见的好多都是引用Masonry插件   ,之后我自己尝试了一个没有使用插件的 <body> <div id="main"> <div cla ...

  4. 理解Object.defineProperty函数中的get与set

    defineProperty是什么: 该函数可以直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象.通俗理解就是: 给对象添加一个新的属性,或者针对对象里的某些属性,可以给这 ...

  5. Spring教程笔记(3) Bean

    Bean配置项 id class必须项 如果根据id来获取bean,要写id:如果根据类型来配置bean,只写class就可以. scope作用域 constructor arguments prop ...

  6. Could not autowire. No beans of 'xxxx' type found的错误

    在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示.但程序的编译和运行都是没有问题的,这个错误提示并 ...

  7. es6,es7,es8

    概述 ES全称ECMAScript,ECMAScript是ECMA制定的标准化脚本语言.目前JavaScript使用的ECMAScript版本为ECMAScript-262. ECMAScript 标 ...

  8. 【Jenkins】控制台输出是中文乱码

    1.问题:查看控制台输出,有的时候,输出信息是中文乱码的 2.解决方法:在环境变量里配置 拷贝出来: JAVA_TOOL_OPTIONS -Dfile.encoding=UTF-8 3.结果:输出信息 ...

  9. pip更新

    python -m ** install -U **

  10. javascript 中的number

    大家都知道javascript中有五种简单数据类型,number,string,boolean,null,undefined,复杂数据类型是object.本文主要记录下number类型的一些可能不太常 ...