搭建一个基于OSGI的可以运行helloworld.html和helloworld.jsp的简单web环境

初学osgi时,搭建环境搞了好长时间也没什么进展,网上的资料参考的也太理想,最终浪费了很时间。这里把我搭建一个简单环境的步骤分享出来,希望对那些初学osgi的伙伴们有所帮助。

由于不同版本的eclipse中内置的osgi版本不同,搭建的时候有所不同,我的环境是eclipse4.2,其中的org.eclipse.osgi版本为3.8.1.v20120830-144521

在开始搭建之前,有个功能必须特别说明一下,那就是Validate Bundles按钮,这个功能可以查看到当前环境缺少的bundle,因此在搭建过程中每次新添加了bundle之后都要去点这个按钮,一旦发现有依赖的bundle未被引入要立即添加

1、创建一个新的OSGi Framework,去除所有默认的bundle,然后先添加最基本的五个bundle
org.eclipse.osgi
org.eclipse.equinox.console
org.apache.felix.gogo.command
org.apache.felix.gogo.runtime
org.apache.felix.gogo.shell
有了这五个,osgi已经可以启动了,启动之后,我们可以通用osgi控制台输入ss回车,查看已经正确启动的bundle

osgi> ss
"Framework is launched."
id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.8.1.v20120830-144521
1 ACTIVE      org.apache.felix.gogo.runtime_0.8.0.v201108120515
2 ACTIVE      org.eclipse.equinox.console_1.0.0.v20120522-1841
3 ACTIVE      org.apache.felix.gogo.command_0.8.0.v201108120515
4 ACTIVE      org.apache.felix.gogo.shell_0.8.0.v201110170705
osgi> 

2、我这里使用jetty容器,添加jetty相关的8个bundle

org.eclipse.equinox.http.jetty
org.eclipse.jetty.continuation
org.eclipse.jetty.http
org.eclipse.jetty.io
org.eclipse.jetty.security
org.eclipse.jetty.server
org.eclipse.jetty.servlet
org.eclipse.jetty.uitl

添加了jetty之后,通过Validata Bundles按钮,可以发现有些jetty依赖的bundle未引入,需要把缺少的bundle也添加进来
javax.servlet
org.eclipse.equinox.http.servlet
org.eclipse.osgi.services

至此一个简单的web环境搭建成功,启动服务后,在浏览器中输入http://localhost,看到以下页面说明环境搭建成功

HTTP ERROR: 404

Problem accessing /. Reason:

    ProxyServlet: /


Powered by Jetty://

注意:jetty的默认端口号是80,如果想更改端口号,需要在VM arguments中添加如下参数-Dorg.osgi.service.http.port=10000"

3、下面要做的是注册资源,也就是让新建的页面可以通过浏览器访问到,我这里使用扩展点的方式注册,当然也有其它方法,比如代码注册

在环境中添加注册资源会用到的bundle,org.eclipse.equinox.http.registry,另外要把该bundle依赖的bundle添加进来
org.clipse.equinox.http.registry
org.eclipse.equinox.common
org.eclipse.equinox.registry
新建立一个插件项目,在根目录下添加文件夹WebContent,在WebContent目录下创建helloworld.html文件,下面进行注册配置,最终要达到的目标就是通过http://localhost/helloworld.html可以访问到页面

打开MANIFEST.MF,在imported packages中添加org.eclipse.equinox.http.registry,引入该包后可配置扩展点,在extensions中添加org.eclipse.equinox.http.registry.resources,点击保存后会自动生成plugin.xml文件,在该文件中添加如下内容

   <extension
         point="org.eclipse.equinox.http.registry.resources">
      <resource alias="/helloworld.html" base-name="/WebContent/helloworld.html" />
   </extension>

这个配置信息的意思就是告诉环境/WebContent就相关于web资源的根目录,重启环境,在浏览器中输入http://localhost/helloworld.html,就可以访问到新创建的页面了。但是,如果这样注册,那每写一个页面都需要添加一条配置信息,那太麻烦了,因此我们把上面的注册信息改进一下
   <extension
         point="org.eclipse.equinox.http.registry.resources">
      <resource alias="/" base-name="/WebContent" />
   </extension>
如此配置后,WebContent下面的所有资源都可以访问了,比如我们在WebContent下建立abc文件夹,在abc文件夹中建立文件abc.html,这时直接在浏览器中输入htttp://localhost/abc/abc.html就可以访问到页面了
4、通过上面的注册,静态资源已经可以正常访问了,比如js、css、html等文件,但是jsp资源还是无法访问的,比如我们在WebContent下面建立一个test.jsp,内容如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String aa = "aa";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
这是jsp的值<%=aa %>
</body>
</html>
实际用浏览器访问到的却是"这是jsp的值",也就是变量aa的值并没有解析出来,原因就是jsp页面需要特别注册才可以
在环境中添加bundle“org.eclipse.equinox.jsp.jasper.registry”,然后添加该bundle依赖的bundle
org.eclipse.equinox.jsp.jasper.registry
org.eclipse.equinox.jsp.jasper
javax.servlet.jsp
javax.el
org.apache.jasper.glassfish
然后在plugin.xml文件中添加如下内容
   <extension
         point="org.eclipse.equinox.http.registry.servlets">
      <servlet alias="/ace/*.jsp" class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/WebContent/ace/" ></servlet>
   </extension>
重启环境,现在就可以访问jsp页面了

至此,一个简单的web环境就搭建起来


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值