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文件丢失的时候会令我们痛苦不已,怎样找回呢?总不能删了吧,那样心血会毁于一旦的,我们肯定不会那样做,那要怎么办呢?我这里提供三种方法: 一,一般情况下这样: 方法一:选中 ...
随机推荐
- hbase之认识
进入HBase客户端命令操作界面 $ bin/hbase shell 查看帮助命令 hbase(main):001:0> help 查看当前数据库中有哪些表 h ...
- 如何使用js改变HTML中title里面固定的文字
document.title = '这里是你想要替换的文字';
- word中一页中添加两种不同的页码
,在文档编写的过程中,可能一个页面需要编写两个不同的页码,举个例子,在页脚有一个页码是整个文档的页码,页眉有一个页码,是每个章节的页码: 设置如下: 此处选中这个图标是为了能够看到分节符和其他的符号 ...
- 使用Dockerfile定制ubuntu+nginx镜像
实验目的:书写Dockerfile,定制ubuntu 14.04 + nginx 1.14.0的镜像. 实验过程: 1. 下载nginx-1.14.0 http://nginx.org/downlo ...
- webAR涉及的技术
1.技术体系 1.1技术体系整理 其中绿色底色的代表Demo中表现出的能力比较成熟,可以直接应用. 脑图地址:http://naotu.baidu.com/file/3392a895a9039 ...
- 第九次java课堂笔记
- java的http请求实例
package vqmp.data.pull.vqmpull.common.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory ...
- redi应用-延时队列
有mq和Kafka,,一般用不上,,两个线程 ..基于Redis 的 list(列表),使用rpush/lpush操作入队列,使用lpop 和 rpop来出队列.
- JDBC-day1
package cn.gzsxt.test; import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.D ...
- [linux-脚本]shebang(shabang #!)
使用Linux或者unix系统的人们对#!这个符号都不陌生,但要说出个具体的所以然来,很多人估计还真不行,我们有必要就此整理一下.Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指 ...