---
title: "To embed OSGi in servlet container"
author: "winse"
date: "2012-07-20T12:44:18Z"
tags: []
categories: ["编程语言"]
slug: "to-embed-osgi-in-servlet-container"
draft: false
lastmod: "2013-02-03T14:37:47Z"
id: 1601916
url: "https://winse.iteye.com/blog/1601916"
---

环境：

Eclipse Modeling Tools

Version: Indigo Service Release 2

Build id: 20120216-1857

 

操作系统：win7

tomcat： apache-tomcat-7.0.27

 

1、 简单操作（在bridge基础上再添加jar）

主页：

[http://eclipse.org/equinox/server/http\_in\_container.php](http://eclipse.org/equinox/server/http_in_container.php)

http://eclipse.org/equinox/server/downloads/bridge.war

基本操作：

[http://developer.51cto.com/art/200910/157948.htm](http://developer.51cto.com/art/200910/157948.htm)

 

2、 通过eclipse product生成**可运行的程序**，然后嵌入到tomcat

（这里需要稍稍的了解类的加载。）

[**demo.zip**](http://dl.iteye.com/topics/download/6efce6f5-d821-3619-a4a7-ae2bbdfaf783) 在 [**sample.http**](http://eclipse.org/equinox/server/downloads/sample-http.zip) 的基础上增加了product打包功能，导入eclipse modeling后，打开/sample.server/server-web.product，点击·Launch an Eclipse application·，然后在浏览器中输入[http://localhost/helloworld](http://localhost/helloworld) (启动后jetty默认端口为80)会看到“Hello World -- sample servlet”。

 

嵌入到tomcat，需要对bridge/WEB-INF/eclipse进行修改。

由于tomcat启动是已经加载了servlet-api（即javax.servlet），同时由于使用tomcat取代jetty作为容器。

1、 如果存在引用javax.servlet bundle。获取该插件的源码，分别打开${BUNDLE}/META-INF/MANIFEST.MF。切换到\`Dependencies\`页签，删除\`Required Plug-ins\`中javax.servlet，在\`Imported Packages\`中添加javax.servlet, javax.servlet.http。 

2、 打开server-web.product，导出可运行的程序。

3、 文件处理

删除导出的 product-export/eclipse/plugins/ 下的 jetty.server 和 javax.servlet 的包。

 

同时，bridge/WEB-INF/eclipse/plugins仅保留org.eclipse.equinox.http.servletbridge和org.eclipse.update.configurator。（其他的包在product-export/eclipse/plugins/已经包含了）

 

拷贝 product-export\\eclipse 下全部的文件到bridge/WEB-INF/eclipse

4、 配置文件bridge/WEB-INF/eclipse/configuration/config.ini

修改加载与启动的bundle。

打开config.ini， org.eclipse.update.configurator和org.eclipse.equinox.http.servletbridge； 删除 javax.servlet 和 org.mortbay.jetty.server 的加载项。

 

（仅进行了粗略的处理，org.eclipse.equinox.launcher应该也还可以去掉）

 

[**all-in-one.zip**](http://dl.iteye.com/topics/download/bf804d0b-6ba0-3edd-9040-13329faab418)处理了后面的三个步骤，运行deploy.bat，然后把all-in-one/bridge/WEB-INF目录（是整个WEB-INF目录）拷贝到webapps/ROOT/目录下即可。

 

![](http://dl.iteye.com/upload/attachment/0071/0635/41f0e54b-336e-31d9-9a3a-dbe01a6e863e.png)

 

 

刚开始遇到同样的问题。在正常启动前，还没有处理掉javax.servlet插件。可能导致加载了不同jar中的的servlet类（javax.servlet bundle与servlet-api jar）：

 

 写道

org.osgi.framework.BundleException: The bundle could not be resolved.  
Reason: Missing Constraint: Import-Package:  
org.eclipse.equinox.servletbridge; version="1.0.0"

 

[http://www.eclipse.org/forums/index.php/mv/tree/34715/](http://www.eclipse.org/forums/index.php/mv/tree/34715/)

 

 

查看的相关文章：

[http://eclipse.org/equinox/server/http\_in\_container.php](http://eclipse.org/equinox/server/http_in_container.php)

[http://www.geclipse.eu/fileadmin/Documents/Guides/deployment.pdf](http://www.geclipse.eu/fileadmin/Documents/Guides/deployment.pdf)

[http://eclipse.dzone.com/articles/embedding-osgi-tomcat](http://eclipse.dzone.com/articles/embedding-osgi-tomcat)

[http://developer.51cto.com/art/200910/157948.htm](http://developer.51cto.com/art/200910/157948.htm)

 

[http://stackoverflow.com/questions/10252768/to-embed-osgi-in-servlet-container-or-servlet-container-in-osgi](http://stackoverflow.com/questions/10252768/to-embed-osgi-in-servlet-container-or-servlet-container-in-osgi)

 

[http://www.blogjava.net/Phrancol/articles/267508.html](http://www.blogjava.net/Phrancol/articles/267508.html)

 

 

源码地址：

:pserver:dev.eclipse.org:/cvsroot/rt,org.eclipse.equinox/server-side/bundles/org.eclipse.equinox.http.servletbridge

:pserver:dev.eclipse.org:/cvsroot/rt,org.eclipse.equinox/server-side/bundles/org.eclipse.equinox.servletbridge

 

https://github.com/winse/hello/tree/ca902237e03c05f13c7a3cf838de48c825d98dc4