Jetty具有嵌入各种应用程序的丰富历史。 在本节中,我们将向您介绍我们的git存储库中的embedded-jetty-examples项目下的一些简单示例。

重要:生成此文档时,将直接从我们的git存储库中提取这些文件。 如果行号不对齐,请随时在github中修复此文档,并向我们提供拉取请求,或者至少打开一个问题以通知我们该差异。

简单的文件服务器

此示例显示如何在Jetty中创建简单文件服务器。 它非常适合需要实际Web服务器来获取文件的测试用例,它可以很容易地配置为从src / test / resources下的目录提供文件。 请注意,这在文件缓存中没有任何逻辑,无论是在服务器内还是在响应上设置适当的标头。 它只是几行,说明了提供一些文件是多么容易。

//
//========================================================================//Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//All rights reserved. This program and the accompanying materials//are made available under the terms of the Eclipse Public License v1.0//and Apache License v2.0 which accompanies this distribution.//
//The Eclipse Public License is available at//      http://www.eclipse.org/legal/epl-v10.html
//
//The Apache License v2.0 is available at//      http://www.opensource.org/licenses/apache2.0.php
//
//You may elect to redistribute this code under either of these licenses.//========================================================================//
packageorg.eclipse.jetty.embedded;importorg.eclipse.jetty.server.Handler;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.handler.DefaultHandler;importorg.eclipse.jetty.server.handler.HandlerList;importorg.eclipse.jetty.server.handler.ResourceHandler;/*** Simple Jetty FileServer.* This is a simple example of Jetty configured as a FileServer.*/
public classFileServer
{public static void main(String[] args) throwsException{//Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0//then a randomly available port will be assigned that you can either look in the logs for the port,//or programmatically obtain it for use in test cases.Server server = new Server(8080);//Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is//a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.ResourceHandler resource_handler = newResourceHandler();//Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.//In this example it is the current directory but it can be configured to anything that the jvm has access to.resource_handler.setDirectoriesListed(true);resource_handler.setWelcomeFiles(new String[]{ "index.html"});resource_handler.setResourceBase(".");//Add the ResourceHandler to the server.HandlerList handlers = newHandlerList();handlers.setHandlers(new Handler[] { resource_handler, newDefaultHandler() });server.setHandler(handlers);//Start things up! By using the server.join() the server thread will join with the current thread.//See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();server.join();}
}

启动后你应该可以导航到http:// localhost:8080 / index.html(假设一个在资源库目录中)。

Maven坐标

要在项目中使用此示例,您需要声明以下Maven依赖项。

<dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>${project.version}</version>
</dependency>

Split File Server

此示例构建在简单文件服务器上,以显示如何将多个ResourceHandler链接在一起可以聚合多个目录以在单个路径上提供内容,以及如何将这些目录与ContextHandler链接在一起。

//
//========================================================================//Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//All rights reserved. This program and the accompanying materials//are made available under the terms of the Eclipse Public License v1.0//and Apache License v2.0 which accompanies this distribution.//
//The Eclipse Public License is available at//      http://www.eclipse.org/legal/epl-v10.html
//
//The Apache License v2.0 is available at//      http://www.opensource.org/licenses/apache2.0.php
//
//You may elect to redistribute this code under either of these licenses.//========================================================================//
packageorg.eclipse.jetty.embedded;importjava.io.File;importorg.eclipse.jetty.server.Connector;importorg.eclipse.jetty.server.Handler;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.ServerConnector;importorg.eclipse.jetty.server.handler.ContextHandler;importorg.eclipse.jetty.server.handler.ContextHandlerCollection;importorg.eclipse.jetty.server.handler.ResourceHandler;importorg.eclipse.jetty.toolchain.test.MavenTestingUtils;importorg.eclipse.jetty.util.resource.Resource;/*** A {@linkContextHandlerCollection} handler may be used to direct a request to* a specific Context. The URI path prefix and optional virtual host is used to* select the context.*/
public classSplitFileServer
{public static void main( String[] args ) throwsException{//Create the Server object and a corresponding ServerConnector and then//set the port for the connector. In this example the server will//listen on port 8090. If you set this to port 0 then when the server//has been started you can called connector.getLocalPort() to//programmatically get the port the server started on.Server server = newServer();ServerConnector connector= newServerConnector(server);connector.setPort(8090);server.setConnectors(newConnector[] { connector });//Create a Context Handler and ResourceHandler. The ContextHandler is//getting set to "/" path but this could be anything you like for//builing out your url. Note how we are setting the ResourceBase using//our jetty maven testing utilities to get the proper resource//directory, you needn't use these, you simply need to supply the paths//you are looking to serve content from.ResourceHandler rh0 = newResourceHandler();ContextHandler context0= newContextHandler();context0.setContextPath("/");File dir0= MavenTestingUtils.getTestResourceDir("dir0");context0.setBaseResource(Resource.newResource(dir0));context0.setHandler(rh0);//Rinse and repeat the previous item, only specifying a different//resource base.ResourceHandler rh1 = newResourceHandler();ContextHandler context1= newContextHandler();context1.setContextPath("/");File dir1= MavenTestingUtils.getTestResourceDir("dir1");context1.setBaseResource(Resource.newResource(dir1));context1.setHandler(rh1);//Create a ContextHandlerCollection and set the context handlers to it.//This will let jetty process urls against the declared contexts in//order to match up content.ContextHandlerCollection contexts = newContextHandlerCollection();contexts.setHandlers(newHandler[] { context0, context1 });server.setHandler(contexts);//Start things up!
server.start();//Dump the server state
System.out.println(server.dump());//The use of server.join() the will make the current thread join and//wait until the server is done executing.//Seehttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join();}
}

启动后,你应该可以导航到http:// localhost:8090 / index.html(假设一个在资源库目录中),你可以正常访问。 任何文件请求都将在第一个资源处理程序中查找,然后在第二个资源处理程序中查找,依此类推。

Maven Coordinates

要在项目中使用此示例,您需要声明以下maven依赖项。 我们建议您不要在实际应用程序中使用工具链依赖项。

<dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>${project.version}</version>
</dependency>
<dependency><groupId>org.eclipse.jetty.toolchain</groupId><artifactId>jetty-test-helper</artifactId><version>2.2</version>
</dependency>

参考资料:http://www.eclipse.org/jetty/documentation/9.4.x/embedded-examples.html

转载于:https://www.cnblogs.com/mymelody/p/9668803.html

Jetty 开发指南:嵌入式开发示例相关推荐

  1. 【嵌入式开发】 嵌入式开发工具简介 (裸板调试示例 | 交叉工具链 | Makefile | 链接器脚本 | eclipse JLink 调试环境)

    作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42239705 参考博客 : [嵌入式开发]嵌入式 开发环境 (远程 ...

  2. 基于软件开发对嵌入式开发的思考

    由于本人专业方向是计算机体系结构方向的,平时做嵌入式方面的实验以及项目较多,这个学期又学习了软件工程的课程,因此想借此机会,总结下在软件工程上面学习到的知识,并看看是否有什么能够借鉴到嵌入式方向的开发 ...

  3. Java开发和嵌入式开发该如何选择

    首先,Java开发和嵌入式开发都是目前IT行业内比较常见的开发岗位,也都有大量的从业人员,所以从就业的角度来看,学习Java开发和嵌入式开发都是不错的选择.Java语言的应用领域包括Web开发.And ...

  4. 单片机开发和嵌入式开发流程图

    单片机开发流程 嵌入式开发流程

  5. 该如何选择Java开发和嵌入式开发

    首先,Java开发和嵌入式开发都是目前IT行业内比较常见的开发岗位,也都有大量的从业人员,所以从就业的角度来看,学习Java开发和嵌入式开发都是不错的选择.Java语言的应用领域包括Web开发.And ...

  6. 2022年入坑,互联网开发和嵌入式开发,你会选择哪个?

    - 前言 - 2022年入坑,互联网开发和嵌入式开发,你会选择哪个?我想很多人会毫不犹豫的选择互联网吧,嵌入式没有那么大众化,没有那么多关注,对它的概念可能没有那么清楚.今天跟大家谈谈我的看法. -  ...

  7. 流媒体服务器开发——SRS 4.0与WebRTC音视频通话丨音视频开发丨嵌入式开发丨FFmpeg丨iOS音视频开发

    SRS 4.0与WebRTC音视频通话 1. 音视频高薪岗位都需要什么技能点 2. WebRTC的技术点分析 3. SRS 4.0如何使用WebRTC 视频讲解如下,点击观看: 流媒体服务器开发--S ...

  8. 解析Linux内核源码中数据同步问题丨C++后端开发丨Linux服务器开发丨Linux内核开发丨驱动开发丨嵌入式开发丨内核操作系统

    剖析Linux内核源码数据同步 1.pdflush机制原理 2.超级块同步/inode同步 3.拥塞及强制回写技术 视频讲解如下,点击观看: 解析Linux内核源码中数据同步问题丨C++后端开发丨Li ...

  9. SRS流媒体服务器架构设计及源码分析丨音视频开发丨C/C++音视频丨Android开发丨嵌入式开发

    SRS流媒体服务器架构设计及源码分析 1.SRS流媒体服务器架构设计 2.协程-连接之间的关系 3.推流-转发-拉流之间的关系 4.如何手把手调试SRS源码 视频讲解如下,点击观看: SRS流媒体服务 ...

  10. 单片机开发和嵌入式开发的区别

    单片机开发和嵌入式开发都是针对嵌入式系统的应用领域,但是两者有着不同的特点和应用场景.在本文中,我们将探讨单片机开发和嵌入式开发的区别,并介绍它们的应用场景和技术特点. 一.单片机开发和嵌入式开发的区 ...

最新文章

  1. 输入和学生成绩的输出
  2. 基于BP弱分类器用Adaboost的强分类器
  3. SpringMVC跳转页面默认类型和转发、重定向的使用
  4. 人工智能“训练员”让 AI 更聪明
  5. 指针作为函数参数引用数组的任意元素
  6. Git服务器报错:host key for (ip地址) has changed and you have requested strict checking
  7. java concurrent包介绍及使用
  8. 作为曾经的 Web 开发“王者”,jQuery 的传奇怎么续写?
  9. 使用Seam Framework + JBoss 5.0 开发第一个Web应用 - 简单投票程序
  10. Win10 64bit安装VC6+VC6助手
  11. 【电子产品】Fast FWR200 公司使用设备人数超过20个后,之后的设备无法上网
  12. tpm_crb MSFT0101:00: [Firmware Bug]: ACPI region does not cover the entire command/re处理
  13. ICML 2020论文笔记:地表最强文本摘要生成模型PEGASUS(天马)
  14. Epicor客制化 - 在VS中进行开发
  15. 1068 万绿丛中一点红(20)
  16. python3 函数类型限制登录可解封_如何解决python反爬虫限制访问?
  17. cesium 加载geojson 贴3dtiles
  18. [开源工具]2022/2023 分享好用的免费的云短信/临时短信[Temp Message]
  19. [转]AP,mAP计算
  20. 南京理工大计算机专业介绍,南京理工大学计算机科学与技术专业介绍

热门文章

  1. 关于python的一些好的书籍推荐-如果只能推荐3本关于python的书,你会推荐哪3本?...
  2. python导入csv文件-Python从CSV文件导入数据和生成简单图表
  3. python是一门什么课程-为什么一定要让孩子学会一门编程语言?
  4. python教学视频下载-Python机器学习入门教程全套视频下载【传智播客】
  5. python开发工程师面试题-一名python web后端开发工程师的面试总结
  6. 想学python编程-【经验分享】新手如何快速学好Python?
  7. python自动测试p-Python自动化测试
  8. python详细安装教程3.8.3-Python下载 v3.8.3 官方中文版
  9. python在线课程-《Python程序设计与应用》在线课程使用说明
  10. python可以从事什么工作-学Python能干什么工作?工作前景怎么样?