`
snoopy7713
  • 浏览: 1123434 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

在equinox环境开发web应用的"利器" -- registerResources()方法 详解

    博客分类:
  • OSGi
阅读更多

registerResources()方法详解

1、简介
registerResources(...)是org.osgi.service.http.HttpService类中提供的方法,可以直接向jetty服务器中注册静态的资源。


2、使用说明
registerResources(String alias, String name, HttpContext context)

A、参数 alias

作用:
           
设置访问内容相对的URL地址

约束:

1)不能为空 null
2)必须以 / 开始
3)可以设置为 /
4)不能以 / 结束
5)不能和已经用过的alias相同,包括使用registeServlet使用的alias
6)支持通配符 * 
7)可以作为过滤器使用,见举例2的用法
8)alias的设定遵循Servlet 2.5规范的规定,下面图中举例说明设置和使用 (参见Servlet规范 2.5种 SRV.3.4 Request Path Elements章节)
如果alias为指定的具体文件,则在name参数(目录)中当中也要指向此文件,否则会出现服务器访问目录而不访问文件的情况,访问目录当然不能得到需要文件的内容,所以会报找不到文件的错误。

B、参数name
作用: 设置alias中目录以及文件在bundle中的位置。
           name实际上是一个目录名称,目录中存在alias中设定的访问内容
           当alias设定为具体的文件名称时,name也需要设定为相应路径下文件的全名
约束:
1)name不能以 / 结尾
2)name可以为 /

C、参数context
作用:context当前bundle的上下文内容。
      如果为空,equinox自动创建一个默认的context。

3、使用举例

举例1:


假设 网站为  http://www.teamlet.org   alias="/test" 
     registerResources("/test", "/", null)
     则通过  http://www.teamlet.org/test   可以访问 test下的所有静态文件内容

举例2:

假设 网站为  http://www.teamlet.org   alias="/test/*.jsp" 
     registerResources("/test/*.jsp", "/", null)
     则通过  http://www.teamlet.org/test   可以访问 test下的所有jsp内容,而所有html等其他静态内容不可以访问


4、ResourceServlet与registerResources的比较

A、相同:
registerResources是HttpService提供的方法,可以直接向jetty服务器中注册静态的资源。
ResourceServlet类通过几个的ServletAdaptor向Jetty服务器注册静态资源。

两者都只允许http请求中的三个请求方法:"GET"、"POST"和"HEAD"使用,其他的http请求方法不允许执行。

HTTP规范定义了8种可能的请求方法:
GET               检索URI中标识资源的一个简单请求
HEAD              与GET方法相同,服务器只返回状态行和头标,并不返回请求文档
POST              服务器接受被写入客户端输出流中的数据的请求
PUT               服务器保存请求数据作为指定URI新内容的请求
DELETE            服务器删除URI中命名的资源的请求
OPTIONS           关于服务器支持的请求方法信息的请求
TRACE             Web服务器反馈Http请求和其头标的请求
CONNECT           已文档化但当前未实现的一个方法,预留做隧道处理

B、区别

registerResources可以直接注册静态资源不需要filter实例,不需要ServletAdaptor适配器。
ResourceServlet可以利用ServletAdaptor设置filter来保护资源,设置字符集等预处理。

5、实用代码:

package  org.teamlet.osgi.test.servlet.filter;

import  java.io.IOException;
import  javax.servlet. * ;
import  javax.servlet.Filter;
import  javax.servlet.FilterConfig;
import  javax.servlet.Servlet;
import  javax.servlet.ServletException;
import  javax.servlet.ServletRequest;

import  org.eclipse.equinox.http.helper.BundleEntryHttpContext;
import  org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import  org.eclipse.equinox.http.helper.FilterServletAdaptor;
import  org.eclipse.equinox.http.helper.ResourceServlet;
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  org.osgi.util.tracker.ServiceTracker;

public   class  Activator  implements  BundleActivator {

 
private  ServiceTracker httpServiceTracker;

 
public   void  start(BundleContext context)  throws  Exception {
  httpServiceTracker 
=   new  HttpServiceTracker(context);
  httpServiceTracker.open();
 }

 
public   void  stop(BundleContext context)  throws  Exception {
  httpServiceTracker.close();
 }

 
private   class  HttpServiceTracker  extends  ServiceTracker {

  
public  HttpServiceTracker(BundleContext context) {
   
super (context, HttpService. class .getName(),  null );
  }

  
public  Object addingService(ServiceReference reference) {
   
final  HttpService httpService  =  (HttpService) context.getService(reference);
   
try  {
    HttpContext commonContext 
=   new  BundleEntryHttpContext(context.getBundle(),  " /webroot " ); 
    httpService.registerResources(
" /jsp/*.jsp " " / " , commonContext); 
    httpService.registerResources(
" /jsp/*.html " " /test " , commonContext);
   } 
catch  (Exception e) {
    e.printStackTrace();
   }
   
return  httpService;
  }

  
public   void  removedService(ServiceReference reference, Object service) {
   
final  HttpService httpService  =  (HttpService) service;
   httpService.unregister(
" /jsp " ); 
   httpService.unregister(
" /jsp/*.jsp " ); 
   
super .removedService(reference, service);
  }   
 }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics