- 浏览: 479813 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
龘龘龘:
TrueBrian 写道有个问题,Sample 1中,为了控制 ...
What's New on Java 7 Phaser -
龘龘龘:
楼主总结的不错。
What's New on Java 7 Phaser -
TrueBrian:
有个问题,Sample 1中,为了控制线程的启动时机,博主实际 ...
What's New on Java 7 Phaser -
liguanqun811:
不知道楼主是否对zookeeper实现的分布式锁进行过性能测试 ...
Distributed Lock -
hobitton:
mysql的get lock有版本限制,否则get lock可 ...
Distributed Lock
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的内容,否则会提示重新输入用户名和密码。
- helper.src.zip (3.3 KB)
- 下载次数: 460
评论
这种注册方式好像只能注册 org.eclipse.equinox.http.helper.Filter 这种类
javax.servlet.Filter 是不能注册的。我的环境是 eclipse 3.5的 不知道大侠有没有试过?
如果我们想对所有JSP进行过滤,这样写:
hs.registerServlet("*.jsp", new FilterServletAdaptor(filter, null, jspServlet), null, null);
但是错误,因为不符合Servlet规范:
java.lang.IllegalArgumentException: Invalid alias '*.jsp'
Equinox有对Filter的真正支持吗?
发表评论
-
Understanding the Hash Array Mapped Trie
2012-03-30 10:36 0mark -
A Hierarchical CLH Queue Lock
2012-01-14 19:01 2120A Hierarchical CLH Queue Lock ( ... -
Inside AbstractQueuedSynchronizer (4)
2012-01-08 17:06 3480Inside AbstractQueuedSynchroniz ... -
Inside AbstractQueuedSynchronizer (3)
2012-01-07 23:37 4633Inside AbstractQueuedSynchroniz ... -
Inside AbstractQueuedSynchronizer (2)
2012-01-07 17:54 6318Inside AbstractQueuedSynchroniz ... -
Inside AbstractQueuedSynchronizer (1)
2012-01-06 11:04 7901Inside AbstractQueuedSynchroniz ... -
Code Optimization
2011-10-14 00:11 1571当前开发人员在进行编码的时候,可能很少关注纯粹代码级别的优化了 ... -
Distributed Lock
2011-08-02 22:02 91461 Overview 在分布式系统中,通常会 ... -
What's New on Java 7 Phaser
2011-07-29 10:15 81581 Overview Java 7的并 ... -
Sequantial Lock in Java
2011-06-07 17:00 21801 Overview Linux内核中常见的同步机 ... -
Feature or issue?
2011-04-26 22:23 121以下代码中,为何CglibTest.intercept ... -
Bloom Filter
2010-10-19 00:41 50351 Overview Bloom filt ... -
Inside java.lang.Enum
2010-08-04 15:40 64211 Introduction to enum J ... -
Open Addressing
2010-07-07 17:59 34101 Overview Open addressi ... -
JLine
2010-06-17 09:11 10962Overview JLine 是一个用来处理控 ... -
ID Generator
2010-06-14 14:45 1644关于ID Generator,想 ... -
inotify-java
2009-07-22 22:58 82241 Overview 最近公 ... -
Perf4J
2009-06-11 23:13 84431 Overview Perf4j是一个用于计算 ... -
Progress Estimator
2009-02-22 19:37 1498Jakarta Commons Cookbook这本书 ... -
jManage
2008-12-22 00:40 39221 Overview 由于项目需要, 笔者开发了一个 ...
相关推荐
osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍osgi介绍
资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...
利用R-OSGi实现分布式OSGi应用 本文通过介绍传统 OSGi 应用程序及 R-OSGi 的实现方式入手,阐述了 R-OSGi 对于 OSGi 规范的实现方式。然后通过一个简单的功能实现由浅入深地讲述传统 OSGi 和 R-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 实例 eclipse 插件开发
OSGi的入门教程,帮助初学者快速了解OSGi的定义、用途及组成。
讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的讲OSGI应用的
osgi,林昊写的osgi实战和进阶,学习osgi的好东西,入门的首选。
OSGI 入门资料PDF,包括OSGI实战、OSGI进阶、OSGI 入门和整合Spring、OSGI原理与最佳实践
本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从OSGi的简介开题,介绍OSGi的作用及基本概念;其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种Java应用;最后...
OSGi的入门资料,网上找的,初探OSGi 的全文
OSGi开发文档和实践指南,描述了OSGI的开发流程
OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文OSGi 中文版 中文 OSGi中文
OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料 OSGi学习不错的材料
介绍OSGI组件编程,详细介绍用eclipse和Equinox开发OSGI程序