OSGi 系列(十二)之 Http Service
OSGi 系列(十二)之 Http Service
1. 原始的 HttpService
(1) 新建 web-osgi 工程,目录结构如下:
(2) HomeServlet
package com.github.binarylei.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("<h1>osgi http service</h1>");
}
}
(3) Activator
package com.github.binarylei.servlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import javax.servlet.Servlet;
import java.util.Dictionary;
import java.util.Hashtable;
public class Activator implements BundleActivator {
private HttpService http = null;
private ServiceRegistration<Servlet> serviceRegistration;
public void start(BundleContext context) throws Exception {
ServiceReference<HttpService> ref = context.getServiceReference(HttpService.class);
if(ref == null) {
System.out.println("http service is null");
} else {
http = context.getService(ref);
HttpContext httpContext = http.createDefaultHttpContext();
//注册servlet
http.registerServlet("/home", new HomeServlet(), null, httpContext);
//注册静态资源
http.registerResources("/static", "web", httpContext);
}
//通过发布服务的方式,注册servlet。不能注册静态资源
Dictionary<String, String> properties = new Hashtable<>();
properties.put("alias", "/home2");
serviceRegistration = context.registerService(Servlet.class, new HomeServlet(), properties);
}
public void stop(BundleContext context) throws Exception {
if(http != null) {
http.unregister("/home");
http.unregister("/static");
}
serviceRegistration.unregister();
}
}
(4) karaf 测试:
先测试 http.registerServlet() 注册方式
feature:install http
注意: 要先启动 http 服务,再启动 web-osgi
再测试 context.registerService(Servlet.class, new HomeServlet(), properties) 注册方式
feature:install http http-whiteboard
2. OSGi 中运行 war 包
(1) 新建 webapp-osgi 工程,目录结构如下:
(2) LoginServlet
@WebServlet("/web/app")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("this is osgi web app");
}
}
(3) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="false">
</web-app>
(4) 配制 war 打包方式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<!-- 表示 MANIFEST.MF 的内容由 maven-bundle-plugin 插件生成 -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Web-ContextPath>/osgiweb</Web-ContextPath>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
</instructions>
</configuration>
</plugin>
(5) 测试:
karaf 默认是不支持 war 包安装方式
feature:install war
OSGi 系列(十二)之 Http Service的更多相关文章
- Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】
2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...
- SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据
原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...
- Alamofire源码解读系列(十二)之请求(Request)
本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...
- OSGi 系列(二)之 Hello World
OSGi 系列(二)之 Hello World 之前曾介绍过 OSGi 是什么,下面将继续上篇介绍的内容,讲述一个简单的 OSGi Bundle:Hello World 是如何开发的. 在 OSGi ...
- struts2官方 中文教程 系列十二:控制标签
介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...
- 爬虫系列(十二) selenium的基本使用
一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...
- Alamofire源码解读系列(十二)之时间轴(Timeline)
本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...
- 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- 打开order by的大门,一探究竟《死磕MySQL系列 十二》
在日常开发工作中,你一定会经常遇到要根据指定字段进行排序的需求. 这时,你的SQL语句类似这样. select id,phone,code from evt_sms where phone like ...
随机推荐
- js 获取上传文件的字节数及内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- django-template-forloop
forloop.counter0 # 是每次循环的index 红色的div标签,居然可以这样写. ex:第一次循环的结果 <div class="item active" ...
- UVA127
模拟游戏,规则如下把卡牌一张接一张,从左到右依次摊开,不可以重叠,每当某张卡片和左边(左边第三张)卡片匹配,它就能放到另外一张卡片上,匹配的规则是他们有一样的级别或者花色,在每次移动完成以后,还需要再 ...
- rsync同步web数据
rsync远程同步web服务器的数据 实验拓扑 服务器A(rsync服务器)--------------服务器B( ...
- JPA和Hibernate到底是什么关系???
转自:https://www.cnblogs.com/mosoner/p/9494250.html 在学习框架的过程中,发现学的东西很多,但是感觉他们之间的联系区别都不是很了解,知道JPA可以去实现持 ...
- django celery 定时任务
可参考上一篇:http://www.cnblogs.com/wumingxiaoyao/p/8515075.html 1. 安装 django-celery-beat pip3 install dja ...
- XE 安装后C盘占用太大,C盘空间清理
XE安装完C盘可以清理的垃圾缓存文件 C:\ProgramData\{0DE47792-19BD-4AF4-B9CF-6378FBA44825} XE6 9个G,xe7 18G XE6如此,XE7安 ...
- SpringMvc 获取ApplicationContext
有时,我们不通过Controller层进入Service层,比如同步数据,任务,以及文件上传共通Handler对文件处理后保存数据等都会由一个非Controller类调用Service. 这时候如果n ...
- UI5-文档-4.21-Data Types
发票清单已经很好看了,但是没有指定价格的发票是什么?通常价格以技术格式存储,并带有'.数据模型中的分隔符.例如,我们的菠萝发票上的计算价格是87.2,没有货币.我们将使用SAPUI5数据类型正确地格式 ...
- Winform 窗体关闭事件
//窗体关闭前事件 private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { DialogResult res ...