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包的形式发布.开发人员可以将 ...
随机推荐
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- poj_1743_Musical Theme(后缀数组)
题目链接:poj_1743_Musical Theme 题意: 给你一串数字,让你找最长的变化相同不重叠的子串,至少长度为5 题解: 处理数据后用后缀数组加二分答案,然后用height数组check答 ...
- OpenLayer
<html> <head> <meta charset="utf-8"> <title>GIS开发样例-V1.0</title ...
- OAuth流程
简介 OAuth是一种协议,OAuth协议为用户资源的授权提供了一个安全的.开放而又简易的标准 第三方若想访问用户资源,就必须遵守服务提供商实现的OAuth协议 OAuth授权的步骤(新浪微博为例) ...
- LINQ的Any() 方法
Enumerable.Any 方法 确定序列中的任何元素是否存在或满足条件.
- XP 右键扩展设置 1.0 免费绿色版
软件名称: xp右键扩展设置软件 1.0 免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 57 ...
- tomcat session失效时间
conf\web.xml <session-config> <session-timeout>600</session-timeout> </session- ...
- Silverlight中如何自己写方法将DataTable转换为PagedCollectionView数据(动态创建类)
将DataTable转换为PagedCollectionView数据,我们可以借用DataTable的GetBindableData()方法,如下: DataTable dt=new DataTabl ...
- Silverlight程序中访问配置文件
以下代码为本人在一Silverlight程序中访问Web端配置文件的代码: private void GetLoadNeed() { // 项目名称读取配置文件 WebClient wcConfigX ...
- [ 订单查询 ] 性能 高并发 : 分表 与 用户id%1024 存放表
逻辑剥离, 保留核心部分 下单 { 核心功能 -- 买家查看订单, 卖家查看收到订单, 修改价格 5个表 附属功能 -- 库存量, 发短信, 给卖家发通知, 订单统计, 销售额统计 } 下单时 一个数 ...