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 ...
随机推荐
- opencv小问题大智慧
opencv易错点记录 1. 判断条件不应使用CV_ASSERT(),而使用CV_Assert(). 2. 用下面这个自适应阈值必须进行数据的转换,不能直接传入数据!不然程序一直崩溃,找了很久才发现 ...
- msq_table's methods
-- 查看有哪些用户 host: % 代表任意地址都可以登录 host: localhost 代表仅本地可以连接select host,user from mysql.user; -- 建库 crea ...
- RedHat7.0更新yum源
https://blog.csdn.net/hongbin_xu/article/details/79316614
- 25. oracle密码过期解决
解决方法: 1.连接到oracle会自动提示修改数据库密码; 2.如果要设置为数据库密码不过期,可以直接修改: 查看:select * from dba_profiles where profile= ...
- JDK1.7之后switch支持string
转自:https://blog.csdn.net/tjcyjd/article/details/9666035 在Java7之前,switch只能支持 byte.short.char.int或者其对应 ...
- Ambari安装Hadoop集群
* System Environment:centOS6.7 1.Prepare the Environment 1)Set Up Password-less SSH : (Generate publ ...
- WDA-FPM-1-Roadmap(GAF)
转载:https://www.cnblogs.com/sapSB/p/10077564.html 首先要有个简单的认识: 1.FPM支持的几种UI配置界面接口: Object Instance Flo ...
- insert NULL into mysql
https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are insertin ...
- spring coud feign
1. 依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...
- redis之RDB持久化与AOF持久化
Redis是一个键值对数据库服务器,服务器中通常包含着任意个非空数据库,而每个非空数据库中又可以包含任意个键值对,为了方便起见,我们将服务器中的非空数据库以及它们的键值对统称为数据库状态. 因为Red ...