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. ubuntu1604 golang环境

    copy来的,这里记录一下 1. 升级系统: sudo apt-get upgrade 2. 安装docker 下载docker-ce: https://download.docker.com/lin ...

  2. 微信小程序分包跳转主包页面

    由于公司项目比较多,我们事业部的微信小程序就在一个分包里.那分包页面要回到主包的首页,该怎么跳转呢,有以下两种方法 wx.switchTab(Object object) 跳转到 tabBar 页面, ...

  3. 九度1456胜利大逃亡【BFS】

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:4432 解决:1616 题目描述: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡 ...

  4. Android CmakeList

    https://www.cnblogs.com/chenxibobo/p/7678389.html

  5. git不提交某个文件

    在版本库中的文件,即使维护在.gitignore也不管用了.要先移除. 比如Constants.java,进入到这个文件目录下: 第一步:git rm -r -n —cached Constants. ...

  6. Bootstrap4 导航栏

    Bootstrap4 导航栏 目录 Bootstrap4 导航栏 动态选项卡 标准的导航栏 导航对齐方式 导航栏的组成 ul 元素中包含navbar-nav 类 表示导航栏中ul li元素中包含nav ...

  7. gevent模块学习(四)

    gevent.spawn会对传入的子任务集合进行调度,gevent.joinall 方法会阻塞当前程序,除非所有的greenlet都执行完毕,才会退出程序 公有方法 gevent.spawn(cls, ...

  8. JSP动作标签flush属性值

    在JSP动作标签<jsp:include flush="true"/>,flush属性可以为true或false.flush默认值为false,当把flush属性赋值为 ...

  9. node遍历文件夹并读取文件内容

    var fs = require('fs'); var path = require('path');//解析需要遍历的文件夹 var filePath = path.resolve('./dist' ...

  10. python学习小总结(列表、元组、字典、集合、字符串)

    ---恢复内容开始--- 一.列表(list) 1.添加 append():追加,在列表末尾添加元素. 列表名.append(添加的元素) extend():扩展,在列表末尾添加元素. 列表名.ext ...