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参数(目录)中当中也要指向此文件,否则会出现服务器访问目录而不访问文件的情况,访问目录当然不能得到需要文件的内容,所以会报找不到文件的错误。

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

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关于服务器支持的请求方法信息的请求
TRACEWeb服务器反馈Http请求和其头标的请求
CONNECT已文档化但当前未实现的一个方法,预留做隧道处理

B、区别

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

5、实用代码:

packageorg.teamlet.osgi.test.servlet.filter;

importjava.io.IOException;
importjavax.servlet.*;
importjavax.servlet.Filter;
importjavax.servlet.FilterConfig;
importjavax.servlet.Servlet;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;

importorg.eclipse.equinox.http.helper.BundleEntryHttpContext;
importorg.eclipse.equinox.http.helper.ContextPathServletAdaptor;
importorg.eclipse.equinox.http.helper.FilterServletAdaptor;
importorg.eclipse.equinox.http.helper.ResourceServlet;
importorg.eclipse.equinox.jsp.jasper.JspServlet;
importorg.osgi.framework.BundleActivator;
importorg.osgi.framework.BundleContext;
importorg.osgi.framework.ServiceReference;
importorg.osgi.service.http.HttpContext;
importorg.osgi.service.http.HttpService;
importorg.osgi.util.tracker.ServiceTracker;

publicclassActivatorimplementsBundleActivator{

privateServiceTrackerhttpServiceTracker;

publicvoidstart(BundleContextcontext)throwsException{
httpServiceTracker
=newHttpServiceTracker(context);
httpServiceTracker.open();
}

publicvoidstop(BundleContextcontext)throwsException{
httpServiceTracker.close();
}

privateclassHttpServiceTrackerextendsServiceTracker{

publicHttpServiceTracker(BundleContextcontext){
super(context,HttpService.class.getName(),null);
}

publicObjectaddingService(ServiceReferencereference){
finalHttpServicehttpService=(HttpService)context.getService(reference);
try{
HttpContextcommonContext
=newBundleEntryHttpContext(context.getBundle(),"/webroot");
httpService.registerResources(
"/jsp/*.jsp","/",commonContext);
httpService.registerResources(
"/jsp/*.html","/test",commonContext);
}
catch(Exceptione){
e.printStackTrace();
}
returnhttpService;
}

publicvoidremovedService(ServiceReferencereference,Objectservice){
finalHttpServicehttpService=(HttpService)service;
httpService.unregister(
"/jsp");
httpService.unregister(
"/jsp/*.jsp");
super.removedService(reference,service);
}
}
}


相关文章:

  • 2021-07-04
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
猜你喜欢
  • 2021-07-07
  • 2021-10-07
  • 2022-12-23
  • 2021-09-17
  • 2021-04-04
  • 2021-11-13
  • 2021-09-30
相关资源
相似解决方案