`
whitesock
  • 浏览: 482008 次
  • 性别: 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(Open Services Gateway Initiative)是一种Java模块化系统,它为开发人员提供了一种动态、模块化的运行时环境。在OSGi中,应用程序被分解为称为“bundle”的独立单元,这些bundle可以相互依赖并独立地加载、...

    OSGi与Web容器的整合

    OSGi定义了一个HttpService,用于支持Web应用。Web Application Bundle (WAB) 是一种特殊的OSGi Bundle,它可以作为一个Web应用运行。WAB包含传统的Web项目元素,如Web-INF目录和web.xml,同时还包含OSGi元数据,如...

    OSGI 开发文档中文的

    OSGI(Open Services Gateway Initiative)是一种开放标准,用于创建可模块化的Java应用程序。它提供了一种灵活的框架,使得开发者可以构建、部署和管理模块化组件,这些组件被称为服务或bundle。OSGI的核心理念是将...

    OSGI 实例eclipse插件开发

    OSGI(Open Services Gateway Initiative)是一种模块化系统和Java服务框架,它允许应用程序由一系列可独立更新和替换的模块组成,这些模块称为“bundle”。在本实例中,我们将探讨如何利用OSGI技术来开发Eclipse...

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

    OSGI(Open Services Gateway Initiative)是一种Java模块化系统,它允许开发者将应用程序分解为一系列可独立部署、更新和交互的服务。林昊所著的《OSGI实战》与《OSGI进阶》是深入理解OSGI技术的重要参考资料,适合...

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

    OSGI组件编程是一种在Java平台上构建模块化应用程序的方法,它由OSGi联盟制定标准,并被广泛应用于企业级软件开发,尤其是对于需要高度可扩展性和动态性的系统。在本教程中,我们将深入探讨如何使用Eclipse和Equinox...

    OSGi入门教程(OSGi Introduce)

    OSGi(Open Services Gateway Initiative)是一种Java平台上的模块化服务框架,它定义了一种标准,使得开发者能够构建可互操作的、动态的、模块化的软件系统。OSGi的核心概念是基于Java的模块化,它的主要目标是为...

    OSGi崁入Servlet的配置步驟

    OSGi(Open Services Gateway Initiative)是一种模块化系统和Java服务框架,它允许开发人员将应用程序分解为一组可独立更新和管理的小型服务组件。在OSGi环境中嵌入Servlet,可以实现更加灵活和动态的Web应用部署。...

    osgi重要文档--osgi核心规范文档,osgi服务文档,osgi-最佳原理与实践(王昊编著,共79页)

    这个压缩包包含了关于OSGI的重要文档,分别是“OSGi R4核心规范文档”、“OSGi服务文档”以及“OSGi-最佳原理与实践”(王昊编著)。下面将详细介绍这些文档所涵盖的关键知识点。 首先,"OSGi R4核心规范文档"是...

    osgi注册servlet.pdf

    在OSGi环境中,Servlet的注册与传统的Web应用程序有所不同,因为OSGi容器(例如Equinox)提供了HttpService,用于处理HTTP请求。 在《你好,OSGi》的这篇文章中,作者探讨了如何在OSGi中开发一个简单的Web应用程序...

    OSGI原理与最佳实践

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

    OSGI 入门资料PDF

    OSGI(Open Services Gateway Initiative)是一种开放标准,用于创建模块化和动态的Java应用程序。它为Java开发人员提供了一个框架,使他们能够构建可热插拔的组件,从而实现更灵活、可扩展和可维护的软件系统。在本...

    OSGi原理与最佳实践(完整版)&OSGi_in_action

    OSGi(Open Services Gateway Initiative)是一种开放标准,用于创建模块化Java应用程序。它提供了一种动态的、可扩展的框架,使得开发人员可以构建、部署和管理软件组件。本资源包含两本书籍:“OSGi原理与最佳实践...

    tomcat嵌入OSGI容器

    标题中的“tomcat嵌入OSGI容器”是指在Apache Tomcat服务器中集成OSGI(Open Service Gateway Initiative)框架,使得Tomcat能够支持模块化的应用程序部署和管理。OSGI是一种Java平台上的服务导向架构,它允许动态地...

    spring osgi相关资源

    Spring OSGi是Spring框架与OSGi(Open Service Gateway Initiative)规范相结合的一种技术,它允许在OSGi容器中运行和管理Spring应用。OSGi是一种模块化系统,为Java应用程序提供了动态部署、版本控制和依赖管理的...

    OSGI.rar_OSGI eclipse_eclipse osgi_java OSGI_osgi

    在"OSGI.rar_OSGI eclipse_eclipse osgi_java OSGI_osgi"这个压缩包中,我们可以找到"OSGI实战.pdf",这很可能是关于如何在Eclipse环境下使用OSGI进行实际开发的详细指南。该书可能会涵盖以下关键知识点: 1. **...

Global site tag (gtag.js) - Google Analytics