OSGI嵌入jetty应用服务器
1、搭建osgi基础环境,参考:https://www.cnblogs.com/dyh004/p/10642383.html
2、引入jetty相关的依赖包
修改jetty启动端口
3、com.kszsa.osgi.hello这个bundle中,引入相关的依赖
4、准备静态页面
jetty.html内容如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jetty说明</title> </head> <body> <h2>这是jetty使用说明</h2> <font color="green">//用来注册诸如表态页面等等</font><br> registerResources(String alias, String name, HttpContext context) <br><br><font color="green">//用来注册servlet类</font><br> registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext context) </body> </html>
5、注册静态资源,修改Activator.java
package com.kszsa.osgi.hello;import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService;public class Activator implements BundleActivator {private static BundleContext context;private HttpService service;static BundleContext getContext() {return context;}/*** 启动bundle*/public void start(BundleContext bundleContext) throws Exception {Activator.context = bundleContext;ServiceReference serviceReference = bundleContext.getServiceReference(HttpService.class.getName());service = (HttpService) bundleContext.getService(serviceReference);// 注册HttpContext httpContext = service.createDefaultHttpContext();// 用来注册诸如表态页面等等// 设置别名,所有对"/osgi"映射到"web"目录service.registerResources("/osgi", "/webpage", httpContext);}/*** 停止bundle*/public void stop(BundleContext bundleContext) throws Exception {service.unregister("/osgi");Activator.context = null;}}
6、启动osgi项目,查看结果,访问http://127.0.0.1:8090/osgi/jetty.html
说明静态资源访问成功。
7、注册servlet资源,新建servlet
package com.kszsa.osgi.servlet;import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osgi.framework.BundleContext; public class PrintNameServlet extends HttpServlet{ private static final long serialVersionUID = -9080875068147052401L; private BundleContext context; public PrintNameServlet(BundleContext context) { super(); this.context = context; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); String name = req.getParameter("name"); System.out.println(name); String s = "Hello,world!"; StringBuilder sb = new StringBuilder(); sb.append("<html><title>Response</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); sb.append("<body>"); sb.append(s); sb.append("</body></html>"); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream(),"UTF-8")); bw.write(sb.toString()); bw.flush(); bw.close(); } }
8、修改修改Activator.java,注册servlet
package com.kszsa.osgi.hello;import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService;import com.kszsa.osgi.servlet.PrintNameServlet;public class Activator implements BundleActivator {private static BundleContext context;private HttpService service;static BundleContext getContext() {return context;}/*** 启动bundle*/public void start(BundleContext bundleContext) throws Exception {Activator.context = bundleContext;ServiceReference serviceReference = bundleContext.getServiceReference(HttpService.class.getName());service = (HttpService) bundleContext.getService(serviceReference);// 注册HttpContext httpContext = service.createDefaultHttpContext();// 用来注册诸如表态页面等等// 设置别名,所有对"/osgi"映射到"web"目录service.registerResources("/osgi", "/webpage", httpContext);// 注册servlet// 设置servlet别名,'/osgi/print"映射到servlet的实现service.registerServlet("/osgi/print", new PrintNameServlet(bundleContext), null, httpContext);}/*** 停止bundle*/public void stop(BundleContext bundleContext) throws Exception {service.unregister("/osgi");Activator.context = null;}}
9、重启osgi,访问http://127.0.0.1:8090/osgi/print
参考地址:https://liugang594.iteye.com/blog/1328050
转载于:https://www.cnblogs.com/dyh004/p/10642407.html
OSGI嵌入jetty应用服务器相关推荐
- OSGI嵌入tomcat应用服务器(gem-web)——资源下载
Gem-Web官网介绍: 官网地址:https://www.eclipse.org/gemini/web/download/milestones.php 1.1. 官方正式发布版 https://ww ...
- jax-ws cxf_走向REST:在Spring和JAX-RS(Apache CXF)中嵌入Jetty
jax-ws cxf 对于服务器核心Java开发人员来说,向世界"展示"的唯一方法是使用API. 今天的帖子都是关于JAX-RS的 :使用Java编写和公开RESTful服务. ...
- 走向REST:在Spring和JAX-RS(Apache CXF)中嵌入Jetty
对于服务器核心Java开发人员而言,向世界"展示"的唯一方法是使用API. 今天的帖子都是关于JAX-RS的 :使用Java编写和公开RESTful服务. 但是,我们不会使用涉 ...
- jetty服务器上运行html页面,web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)...
自制Jetty服务类 这种方式可以支持websocket,如果项目中需要使用到可以试试这种. 首先pom.xml引入jetty的依赖: org.eclipse.jetty.aggregate jett ...
- 常用的java应用服务器大概介绍
何为应用服务器呢?它主要为应用程序提供运行环境,为组件提供服务.想进一步了解,可以查看我的博客:何为容器? Java 的应用服务器很多,从功能上分为两类:WEB 应用服务器和 Java EE 应用服 ...
- JAVA应用服务器都有那些?
从功能上看分为两种:web应用服务器和java EE服务器 web服务器 * Tomcat.Jetty.Orion.Resin. Bejy Tiger.Geronimo.Jonas.Jrun java ...
- Java使用Jetty实现嵌入式Web服务器及Servlet容器
Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...
- Jetty开发指导:框架
Spring设置 你能嵌入Jetty到你的项目中,也能够使用差点儿全部的IoC类型框架,包含Spring.假设全部你想做的是在你的Spring中设置Jetty Server,那么以下的xml片段能够作 ...
- 谷歌服务器——为什么选择Jetty?
google服务器为什么选选择Jetty? 因为:Jetty是Java领域另一个出色的Web服务器,它同样也是开源的.与tomcat不同的是它可作为一个嵌入式服务器.也就是说,如果我们在应用中加入je ...
最新文章
- source命令与 .命令
- delphi中的函数传参如何传枚举参数_Python基础笔记Day05函数
- 如何利用脚本方法清除文本框中汉字,从而保留英文字母和数字等
- npm中强制关闭node.js
- 每日一道python的leetcode:冒泡排序
- JAVA基础(4/17)-基本语法_流程控制
- 经济学有必要学python吗_学习经济学用啥软件
- 深度学习——卷积神经网络原理解析
- 你离顶尖Java程序员,只差这11本书的距离 172 分享 分享到新浪微博 分享到QQ空间
- 3.操作系统有五大功能
- 2018-03-08,模板消息推送,全代码,多多指教
- 《从零开始的 RPG 游戏制作教程》第五期:制作物品和技能
- HTML5特效(shadow、gradient、transition、transform、filter)
- 利用华硕路由器实现创维电视广告屏蔽
- MATLAB | 中秋节 · 绘制《山间秋月》及《皓月当空》
- 543、RabbitMQ详细入门教程系列 -【Confirm与Mandatory】 2022.09.05
- adsafe for linux,Adsafe for Mac-Adsafe mac版下载 V1.0-PC6苹果网
- 诸神之战在星际争霸1的实现[001]AI游戏的发端
- Linux 之父:对不起,我错了!
- 确实, 5G与物联网离不开区块链!
热门文章
- 一段简单的python代码_Python趣味打怪:60秒学会一个例子,147段简单代码助你从入门到大师 | 中文资源...
- PHP包含文件函数include、include_once、require、require_once区别
- qgraphicsview 添加矩形框并拖动改变大小_如何用手机巧影软件为视频添加字幕
- python怎么用matplotlib画,用matplotlib在python中绘制OHLC图
- qos 流控功能_怎么设置飞鱼星QoS流量控制中的传统流控
- mysql create很多table,SQL CREATE TABLE 语句
- docker swarm MySQL_容器与云|在 Docker 中运行 MySQL:多主机网络下 Docker Swarm 模式的容器管理...
- c++ 命名规则 private_【译】代码中如何写出更有意义的命名
- 南昌理工学院的计算机科学与技术专业怎么样,南昌理工学院有哪些专业及什么专业好...
- linux内存管理策略,Glibc内存管理—ptmalloc内存分配策略(1)