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的更多相关文章

  1. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

  2. SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据

    原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...

  3. Alamofire源码解读系列(十二)之请求(Request)

    本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...

  4. OSGi 系列(二)之 Hello World

    OSGi 系列(二)之 Hello World 之前曾介绍过 OSGi 是什么,下面将继续上篇介绍的内容,讲述一个简单的 OSGi Bundle:Hello World 是如何开发的. 在 OSGi ...

  5. struts2官方 中文教程 系列十二:控制标签

    介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...

  6. 爬虫系列(十二) selenium的基本使用

    一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...

  7. Alamofire源码解读系列(十二)之时间轴(Timeline)

    本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...

  8. 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  9. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  10. 打开order by的大门,一探究竟《死磕MySQL系列 十二》

    在日常开发工作中,你一定会经常遇到要根据指定字段进行排序的需求. 这时,你的SQL语句类似这样. select id,phone,code from evt_sms where phone like  ...

随机推荐

  1. selector 模块

    selector 模块进行自动匹配,当是windows系统时,自动匹配select, linux系统时,自动匹配epoll,传输时会明确告诉内核响应服务器的地址,速度要更快 import select ...

  2. ABAP-长文本处理

  3. xslt基础学习

    今天下午工作完成没事,登w3c的网站学习了一下xslt的基础知识,主要是因为工作中xml用的比较多,xslt也有用到,所以在这里学习一下. XSLT:一种用于转换 XML 文档的语言. XSLT 用于 ...

  4. Rsync+sersync实现数据实时同步

    前言: 一.为什么要用Rsync+sersync架构? 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增 ...

  5. keras做DNN

    输入的数据集是10000行,31645列,其中前31644是特征,最后一列是标签值.训练集和测试集格式是一样的. 特征值都是0,1形式,表示有还是没有这个特征,标签值是0,1形式,2分类. impor ...

  6. HTTP request failed! HTTP/1.1 411 Length Required

    $opts = array( 'http'=>array( 'method'=>"POST", 'header' => 'Content-Length: 0' / ...

  7. 导出 java.io.IOException: 权限不够

    项目原先都是开发使用root账号登陆服务器,人肉部署. 今天改成了自动部署,部署之后发现导出用不了了,查看服务器日志提示: 查看项目启动用户是app,推断是app用户的权限不够,导致导出无法在服务器创 ...

  8. BlockingQueue深入解析-BlockingQueue看这一篇就够了

    本篇将详细介绍BlockingQueue,以下是涉及的主要内容: BlockingQueue的核心方法 阻塞队列的成员的概要介绍 详细介绍DelayQueue.ArrayBlockingQueue.L ...

  9. MySql log_bin

    [MySql log_bin] 1.查看 log_bin 是否启用. 默认情况下,mysql server 不启用 binlog(验证方法1: 执行"show variables" ...

  10. shell脚本通过expect脚本实现自动输入密码(使用expect)

    背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现   一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具. ...