当前位置: 首页 > news >正文

jetty的使用

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

首先jetty是啥呢?Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。——百度百科

说白了,jetty可以像Tomcat一样让运行web应用。

有什么好处呢?jetty可以很方便的设置项目名和访问端口,在你需要启动多个项目的时候就很方便了。

怎么使用呢?接下来说说怎么在项目中嵌入jetty

首先是pom文件中需要添加:

        <dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>9.2.10.v20150310</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
  		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>9.2.10.v20150310</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

其次是jetty的启动类:

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年4月7日下午3:41:18
 *
 */
public class JettyServer {
	private static final Logger LOGGER = Logger.getLogger(JettyServer.class);
	private int port;
	private String contextPath;
	private String webappPath;
	private Server server;
	private WebAppContext webapp;

	/**
	 * 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
	 */
	public static void main(String[] args) {
		JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81);
		jettyServer.start();
	}

	public JettyServer(String contextPath, String webappPath, int port) {
		if (StringUtils.isBlank(contextPath)) {
			this.contextPath = "";
		} else {
			this.contextPath = contextPath;
		}
		if (StringUtils.isBlank(webappPath)) {
			throw new IllegalArgumentException("webappPath is required");
		} else {
			this.webappPath = webappPath;
		}
		this.port = port;
	}

	public void start() {
		if (null == server || server.isStopped()) {
			doStart();
		} else {
			throw new IllegalStateException("JettyServer already started.");
		}
	}

	private void doStart() {
		if (!checkServerPortAvailable()) {
			throw new IllegalStateException("Server port already in use: " + port);
		}

		server = new Server();
		// 设置在JVM退出时关闭Jetty的钩子。
		server.setStopAtShutdown(true);
		// 这是http的连接器
		ServerConnector connector = new ServerConnector(server);
		connector.setPort(port);
		// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.
		connector.setReuseAddress(false);
		server.setConnectors(new Connector[] { connector });

		webapp = new WebAppContext(webappPath, contextPath);

		server.setHandler(webapp);
		try {
			long st = System.currentTimeMillis();
			server.start();
			long sp = System.currentTimeMillis() - st;
			System.out.println("JettyServer " + Jetty.VERSION + " started: " + String.format("%.2f sec", sp / 1000D)
					+ ",the port is ===" + port + "");
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("JettyServer started failed!");
		}
	}

	private boolean checkServerPortAvailable() {
		if (0 < port) {
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(port, 0, null);
			} catch (Exception e) {
				LOGGER.error("check serverPort failed", e);
				return false;
			} finally {
				if (null != ss) {
					try {
						ss.close();
					} catch (IOException e) {
						LOGGER.error("close ServerSocket failed", e);
					}
				}
			}
		} else {
			throw new IllegalArgumentException("Invalid port " + port);
		}
		return true;
	}

}

需要注意的是:JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81);这个方法中ps是你访问的项目名,81是访问端口,即你要访问项目的地址是:localhost:81/ps

转载于:https://my.oschina.net/u/3534905/blog/1790770

相关文章:

  • mysql架构
  • 详解node.js中的可读流(Readable)和可写流(Writeable)
  • 一文看懂JeffDean等提出的ENAS到底好在哪?
  • MXNet 作者李沐:用深度学习做图像分类,教程+代码
  • Map集合、散列表、红黑树介绍
  • centos7.4系统的虚拟机网络配置教程
  • win10 php安装redis 扩展
  • 6、通过Appium Desktop 实现录制功能
  • 文件上传漏洞攻击
  • Micropython TPYBoard V10X拼插编程实践之定时器 代码不精通?...
  • 从抖音关闭评论,看服务治理的重要性
  • diango-团队介绍
  • ModeBusRtu概述
  • Project中最常用的注意点
  • 云时代,ERP选型莫走入低价、免费误区
  • 4. 路由到控制器 - Laravel从零开始教程
  • github指令
  • Hexo+码云+git快速搭建免费的静态Blog
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • iOS小技巧之UIImagePickerController实现头像选择
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Java超时控制的实现
  • java取消线程实例
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Otto开发初探——微服务依赖管理新利器
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • Spark RDD学习: aggregate函数
  • 聚簇索引和非聚簇索引
  • 面试总结JavaScript篇
  • 使用common-codec进行md5加密
  • 网络应用优化——时延与带宽
  • 微信开源mars源码分析1—上层samples分析
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • Spring第一个helloWorld
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • #{} 和 ${}区别
  • #微信小程序:微信小程序常见的配置传旨
  • (备忘)Java Map 遍历
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • .NET CLR基本术语
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .Net MVC + EF搭建学生管理系统
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • [ CTF ] WriteUp-2022年春秋杯网络安全联赛-冬季赛
  • [30期] 我的学习方法
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息