jetty作为内嵌服务器自启动
为了完成web工程的测试,最近内嵌jetty也要搞起来.第一次搞还是挺焦头烂额的.直接上成果:
package com.test.action; import java.io.File; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext; public class RunTest {
public static void main(String[] args) {
tt();
} public static void tt() { Server server = new Server();// 创建jetty web容器
server.setStopAtShutdown(true);// 在退出程序是关闭服务 // 创建连接器,每个连接器都是由IP地址和端口号组成,连接到连接器的连接将会被jetty处理
Connector connector = new SelectChannelConnector();// 创建一个连接器
connector.setHost("127.0.0.1");// ip地址
connector.setPort(8080);// 连接的端口号
server.addConnector(connector);// 添加连接
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMaxThreads(3000);
server.setThreadPool(threadPool);
// 配置服务
WebAppContext context = new WebAppContext();// 创建服务上下文
context.setContextPath("/strutsDemo");// 访问服务路径 http://{ip}:{port}/
context.setConfigurationDiscovered(true);
String baseDir = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
context.setDescriptor(baseDir + File.separator + "/WEB-INF/web.xml");// 指明服务描述文件,就是web.xml
// context.setDescriptor("/E:/workspace/strutsDemo/target/classes/\\/WEB-INF/web.xml");//
// 指明服务描述文件,就是web.xml
context.setResourceBase(System.getProperty("user.dir")
+ "/src/main/webapp/");// 指定服务的资源根路径,配置文件的相对路径与服务根路径有关
server.setHandler(context);// 添加处理try {
server.start();// 开启服务
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
} }
启动访问就可以了.主要是context的配置花了很多功夫,老是配不好.还有据说jetty版本或者jar包不同会有配置差异,我用的是8.0.4版本,顺便贴上maven依赖:
<!--jetty -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-ajp</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
</dependency>
事实上不需要这么多,我这是一劳永逸的偷懒做法,你们可以改进哟
jetty作为内嵌服务器自启动的更多相关文章
- 使用jetty作为内嵌服务器启动项目
http://blog.csdn.net/robinpipi/article/details/7557035 需求:把jetty作为内嵌的一个服务器,直接启动,web项目不用部署在应用服务器中.在网上 ...
- Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri
文章目录 1. Jetty 的切换 2. Undertow的使用 Spring Boot 可选择内嵌 Tomcat.Jetty 和 Undertow,因此我们不需要以 war 包形式部署项目.< ...
- 使用ActiveMQ 传输文件 以及使用Jetty搭建内嵌文件服务器
使用Active发送文件 ActiveMq 本身提供对于传输文件的支持. 1. 直接传输文件: 使用connection.createOutputStream 的形式.这种方式适合小文件.不能传输大文 ...
- 【spring实战第五版遇到的坑】4.2.3中LDAP内嵌服务器不启动的问题
按照4.2.3中的指导一步一步的去做,在登录界面进行登录时,报错了,报错信息是LDAP服务器连接不上. 后来查了一些资源发现还需要加入一些其他的依赖,如下: <dependency> &l ...
- IDEA内嵌Jetty启动SpringMvc项目
这段时间本意是想要研究一下Netty的多线程异步NIO通讯框架,看完原理想要做下源码分析.查找资料发现Jetty框架底层支持用Netty做web请求的多线程分发处理,于是就筹备着将Jetty框架内嵌到 ...
- 第03篇. 标准Web项目Jetty9内嵌API简单启动
一直以来,想改变一些自己早已经习惯的事情. 到了一定年龄,便要学会寡言,每一句话都要有用,有重量. 喜怒不形于色,大事淡然,有自己的底线. --胖先生 昨天,简单的说了一下关于Jetty9的配置,大家 ...
- Message高级特性 & 内嵌Jetty实现文件服务器
1. Messaage Properties 常见属性 更多的属性以及介绍参考:http://activemq.apache.org/activemq-message-properties.html ...
- Jetty 开发指南:Jetty 内嵌开发
Jetty的口号是“不要在Jetty中部署你的应用程序,在你的应用程序中部署Jetty!” 这意味着,作为将应用程序捆绑为要部署在Jetty中的标准WAR的替代方案,Jetty旨在成为一个软件组件,可 ...
- spring内嵌jetty容器,实现main方法启动web项目
Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...
随机推荐
- c# socket传输struct类型
data结构体类型 public struct datas { public string test1; public string test2; } //socket服务器端 publi ...
- 用getBoundingClientRect()来获取页面元素的位置
以前绝大多数的使用下面的代码来获取页面元素的位置: [code="javascript"]var _x = 0, _y = 0;do{_x += el.offsetLeft;_y ...
- MVC3+EF4.1学习系列(八)-----利用Repository and Unit of Work重构项目
项目最基础的东西已经结束了,但是现在我们的项目还不健全 不利于测试 重复性代码多 层与层之间耦合性高 不利于扩展等问题.今天的这章 主要就是解决这些问题的.再解决这些问题时,自己也产生了很多疑 ...
- 一步一步学EF系列3【升级篇 实体与数据库的映射】
之前的三张为基础篇,如果不考虑架构问题,做一般的小程序,以足够用了.基本的增删改查也都有了.但是作为学习显然是不够的.通过之前三章的学习,有没有发现这样写有什么问题,有没有觉得繁琐的?可能有人会说,之 ...
- C# 语言规范_版本5.0 (第5章 变量)
1. 变量 变量表示存储位置.每个变量都具有一个类型,用于确定哪些值可以存储在该变量中.C# 是一种类型安全的语言,C# 编译器保证存储在变量中的值总是具有合适的类型.通过赋值或使用 ++ 和 ‑‑ ...
- 使用Cookie来统计浏览次数,当天重复刷新不增加
这是一种不严谨的做法,在浏览量不是很重要的时候可以使用 var oldCookie = Request.Cookies["newsCookie"]; if (oldCookie = ...
- Codeforces Round #256 (Div. 2) B Suffix Structures
Description Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" t ...
- hdu 1407 测试你是否和LTC水平一样高
Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Inpu ...
- html5 让IE6,7支持HTML5语义化标签的文件
https://github.com/aFarkas/html5shiv/blob/master/src/html5shiv.js 只要应用这个js就行了
- CreateCompatibleBitmap 需要注意的问题
不要使用CreateCompatibleDC得到的内存DC作为其参数,应使用真实DC,否则图片不能显示