`
whitesock
  • 浏览: 479116 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

OSGi Embedded HttpService

    博客分类:
  • SE
阅读更多

1  概述
    Equinox 提供了两种OSGi embedded HttpSerivce的实现,如下:

 

  • org.eclipse.equinox.http。适合资源受限的环境。兼容Servlet 2.4,但是对Servlet 2.1以外的API提供了有限的支持。
  • org.eclipse.equinox.http.jetty。使用Jetty 作为引擎,支持Servlet 2.4。

    本文以使用org.eclipse.equinox.http.jetty为例,介绍一下通过Eclipse3.3.1.1编写基于bundles的web应用程序。有两种方式在Equinox中运行HTTP Server,如下(本文采用第一种方式):

  • Embed a server in Equinox。
  • Embed Equinox in an existing servlet container。

2  创建工程
    首先在Eclipse中新建一个新工程,New->Project->Plug-in Development->Plug-in Project,单击Next按钮;输入工程名:HttpServiceExample。在Target Platform中选择an OSGi framework单选按钮,同时在右边的下拉框中选择standard,单击Next按钮;在Classpath中输入bin,在Activator 中输入com.example.http.service.Activator,单击Next按钮;单击Finish按钮。
    用Plug-in Manifest Editor打开工程META-INF目录下的MANIFEST.MF文件。选择Dependencies标签,单击Required Plug-ins下的Add按钮。在弹出的对话框中选择以下plug-in:

  • javax.servlet
  • javax.servlet.jsp
  • org.eclipse.osgi.services
  • org.eclipse.equinox.http.jetty
  • org.eclipse.equinox.http.servlet
  • org.eclipse.equinox.http.registry
  • org.eclipse.equinox.jsp.jasper
  • org.eclipse.equinox.jsp.jasper.registry
  • org.mortbay.jetty
  • org.apache.jasper
  • org.apache.commons.el
  • org.apache.commons.logging

3 新建web资源
    在工程的com.example.http.service包中应该已经生成了一个Activator类,为其添加两条控制台输出并注册资源。需要注意的是,BundleEntryHttpContext.java和ContextPathServletAdaptor.java是在名为org.eclipse.equinox.http.helper的bundle中,而这个bundle需要到eclipse cvs下载。为了方便读者,笔者将两个类的源码下载并保存到com.example.http.service.helper包中。

package com.example.http.service;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;

import com.example.http.service.helper.BundleEntryHttpContext;
import com.example.http.service.helper.ContextPathServletAdaptor;
import com.example.http.service.helper.FilterServletAdaptor;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		System.out.println("starting bundle...");
		
		ServiceReference sr = context.getServiceReference(HttpService.class.getName());
		HttpService hs = (HttpService) context.getService(sr);
		
		HttpContext hc = new BundleEntryHttpContext(context.getBundle(), "/web");
        hs.registerResources("/jsp", "/", hc);
         
		Servlet jspServlet = new ContextPathServletAdaptor( new JspServlet(context.getBundle(), "/web/"), "/jsp");
        hs.registerServlet("/jsp/*.jsp", jspServlet, null, hc);
        
        // Filter
        Filter filter = new Filter() {
        	public void init(FilterConfig arg0) throws ServletException {
			}
        	
			public void destroy() {
			}

			public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) 
			throws IOException, ServletException {
				System.out.println("in filter.doFilter()");
				fc.doFilter(request, response);
			}
        };
        hs.registerServlet("/jsp/hello.jsp", new FilterServletAdaptor(filter, null, jspServlet), null, null);
	}

	public void stop(BundleContext context) throws Exception {
		System.out.println("stoping bundle...");
		ServiceReference sr = context.getServiceReference(HttpService.class.getName());
		HttpService hs = (HttpService) context.getService(sr);
        hs.unregister("/web");
        hs.unregister("/jsp");
	}
}

   另外再新建一个LoginServlet,如下:

package com.example.http.service;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1300648117298008054L;

	/**
	 * 
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
		//
		String user = request.getParameter("user");
		String password = request.getParameter("password");
		if("user".equals(user) && "123456".equals(password)) {
			request.setAttribute("message", "hello " + user);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/hello.jsp");
			dispatcher.forward(request, response);
		} else {
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/web/login.html");
			dispatcher.forward(request, response);
		}
	}
}

    在工程根目录下新建一个web目录,在其中新建一个login.html如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
  <br>
  <form name="loginForm" method="post" action="/login">
  	<table>
  		<tr>
  			<td><div align="right">User Name:</div></td>
  			<td><input type="text" name="user"></td>
  		</tr>
  		<tr>
  			<td><div align="right">Password:</div></td>
  			<td><input type="password" name="password"></td>
  		</tr>
  		<tr>
  			<td></td>
  			<td><input type="Submit" name="Submit" value="Submit"></td>
  		</tr>
  </form>
</body>
</html>

    再新建一个hello.jsp,如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>hello</title>
</head>
<body>
<h2><%=request.getAttribute("message") %></h2>
</body>
</html>

 

4 扩展点
    在工程根目录下新建一个plugin.xml文件,内容如下:

<plugin>

  <extension point="org.eclipse.equinox.http.registry.resources">
    <resource
      alias="/web"
      base-name="/web"/>
  </extension>
  
  <extension point="org.eclipse.equinox.http.registry.servlets">
    <servlet
      alias="/login"
      class="com.example.http.service.LoginServlet"/>
  </extension>
  
</plugin>

   修改MANIFEST.MF文件,在Bundle-SymbolicName: HttpServiceExample后边加上;singleton:=true,如下

   Bundle-SymbolicName: HttpServiceExample;singleton:=true 

5 运行
    Run->Open Run Dialog,首先在弹出对话框中,单击右侧的Deselect All。然后选择:

  • HttpServiceExample
  • javax.servlet
  • org.eclipse.osgi.services
  • org.eclipse.equinox.http.jetty
  • org.eclipse.equinox.http.servlet
  • org.eclipse.equinox.http.registry
  • org.eclipse.equinox.jsp.jasper
  • org.eclipse.equinox.jsp.jasper.registry
  • org.mortbay.jetty
  • org.apache.jasper
  • org.apache.commons.el
  • org.apache.commons.logging

   再单击Validate Bundles按钮,如果提示还缺少bundles,那么就根据提示添加bundles。直到提示No problems were detected,单击Run。    

   控制台中除了其它bundles的输出外,还应该有HttpServiceExample的输出:starting bundle...。在控制台输入ss并回车,应该看到所有的state都是ACTIVE。打开浏览器访问http://localhost/web/login.html,输入用户名user,密码123456后会进入显示hello.jsp的内容,否则会提示重新输入用户名和密码。

11
0
分享到:
评论
9 楼 xiaoasha 2014-09-21  
请问如果是以 •Embed Equinox in an existing servlet container 的方式,httpservice的实现与本文中的一致么,或者能否继续提供些实例、资料学习下。非常感谢
8 楼 xiaoasha 2014-09-21  
终于找到一篇比较实用的关于osgi httpserice的文章,楼主你TM太帅了
7 楼 twentygjh 2011-10-28  
首先表示感谢!我通过Eclipse CVS获得org.eclipse.equinox.http.helper bundle后,运行配置时给我抛出java.lang.NoClassDefFoundError: org/eclipse/equinox/http/helper/BundleEntryHttpContext,表示找到了但不是所需的。我下载你的源码编译后,再运行配置,成功!
6 楼 java_feng 2011-06-05  
hs.registerServlet("/jsp/hello.jsp", new FilterServletAdaptor(filter, null, jspServlet), null, null);
这种注册方式好像只能注册 org.eclipse.equinox.http.helper.Filter 这种类

javax.servlet.Filter 是不能注册的。我的环境是 eclipse 3.5的 不知道大侠有没有试过?
5 楼 Aspen 2010-06-30  
虽然Filter得到了执行,但是只能实现对/jsp/hello.jsp的过滤。

如果我们想对所有JSP进行过滤,这样写:
hs.registerServlet("*.jsp", new FilterServletAdaptor(filter, null, jspServlet), null, null);  

但是错误,因为不符合Servlet规范:
java.lang.IllegalArgumentException: Invalid alias '*.jsp'


Equinox有对Filter的真正支持吗?
4 楼 arthurln 2008-10-24  
不好意思,我把jsp文件放到web路径下了,mime type是text/plain所以被firefox直接当文本解析了:)
3 楼 arthurln 2008-10-24  
在IE6下可以正常显示jsp页面,但是在firefox3中把页面代码都显示出来了,怎么解决
2 楼 whitesock 2008-03-05  
代码修改了, Filter部分见Activator。目前只能针对单个地对servlet添加filter。由于org.eclipse.equinox.http.helper目前还是incubating的状态,因此这部分将来可能还会有变更。
1 楼 gembin 2008-03-05  
怎么实现HttpService 的Filter功能

相关推荐

    osgi介绍osgi介绍

    osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍

    OSGI原理与最佳实践

    资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...

    利用R-OSGi实现分布式OSGi应用

    利用R-OSGi实现分布式OSGi应用 本文通过介绍传统 OSGi 应用程序及 R-OSGi 的实现方式入手,阐述了 R-OSGi 对于 OSGi 规范的实现方式。然后通过一个简单的功能实现由浅入深地讲述传统 OSGi 和 R-OSGi 上的两种不同...

    OSGI合集 OSGi原理与最佳实践

    网上收集的OSGI资料. 包括: OSGi原理与最佳实践(精选版).pdf OSGI实战和源码.rar osgi进阶.pdf Introduce.OSGi.ppt OSGi.in.action.ppt r4.cmpn.pdf r4.core.pdf r4.enterprise.pdf

    《OSGi实战》完整中文版

    《 OSGi实战》是学习OSGi的全面指导,利用与架构和开发人员相关的示例清楚地讲解OSGi概念,同时探讨了很多实践场景和技术,阐述了开发人员有多需要OSGi,怎么将OSGi嵌入其他容器中,将遗留系统移入OSGi的最佳实践,...

    OSGI 实例eclipse插件开发

    OSGI 实例 eclipse 插件开发

    OSGi入门教程(OSGi Introduce)

    OSGi的入门教程,帮助初学者快速了解OSGi的定义、用途及组成。

    OSGI进阶.pdf

    讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的

    osgi,林昊写的osgi实战和进阶

    osgi,林昊写的osgi实战和进阶,学习osgi的好东西,入门的首选。

    OSGI 入门资料PDF

    OSGI 入门资料PDF,包括OSGI实战、OSGI进阶、OSGI 入门和整合Spring、OSGI原理与最佳实践

    OSGi原理与最佳实践

    本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从OSGi的简介开题,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种Java应用;最后...

    OSGi入门资料-初探OSGi 的全文

    OSGi的入门资料,网上找的,初探OSGi 的全文

    OSGI进阶--OSGi开发指南

    OSGi开发文档和实践指南,描述了OSGI的开发流程

    OSGi规范(r4)中文版

    OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文

    OSGI进阶实战教程

    OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料

    OSGI组件编程(osgi.component.programming)

    介绍OSGI组件编程,详细介绍用eclipse和Equinox开发OSGI程序

Global site tag (gtag.js) - Google Analytics