将web容器置于OSGi框架下进行web应用的开发

将web容器置于OSGi框架下,其实就是将web容器做成OSGi支持的Bundle,再安装到OSGi框架中,这里使用的是Jetty容器。下面作详细的介绍。

1、创建一个Eclipse插件项目,在此插件下创建一个WebRoot文件夹,里面创建两个文件夹一个是images,一个是pages,里面分别放置一个图片文件,一个简单的html页面和一个简单的jsp文件。大致目录结构如下图所示:

2、jsp文件的内容如下:

Html代码   收藏代码
  1. < %@ page  language = "java"   contentType = "text/html; charset=UTF-8"   pageEncoding = "UTF-8" % >   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  3. < html >   
  4. < head >   
  5. < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" >   
  6. < title > Insert title here </ title >   
  7. </ head >   
  8. < body >   
  9.     < img   src = "images/08.jpg"   width = "200"   height = "150" >   
  10. </ body >   
  11. </ html >   

 
3、编辑MANIFEST.MF文件,将插件使用到的组件和包引入,最终MANIFEST.MF文件的内容如下:

Xml代码   收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: Osgi_jetty Plug-in  
  4. Bundle-SymbolicName: com.cjm.osgi.jetty;singleton: = true   
  5. Bundle-Version: 1.0.0  
  6. Import-Package: javax.servlet,  
  7.  javax.servlet.http,  
  8.  org.osgi.framework;version = "1.3.0"   
  9. Require-Bundle: org.eclipse.equinox.http.registry,  
  10.  org.eclipse.equinox.jsp.jasper.registry  

    当插件使用到Eclipse的扩展点机制时,必须在Bundle-SymbolicName属性值最后设置singleton的值为true。

    对于Servlet需要导入以下包:

         javax.servlet

         javax.servlet.http

    对于jsp等web资源需要引入以下插件:

         org.eclipse.equinox.http.registry

         org.eclipse.equinox.jsp.jasper.registry

4、servlet的源码如下:

Java代码   收藏代码
  1. public   class  HelloWorldServlet  extends  HttpServlet {  
  2.     private   static   final   long  serialVersionUID = -5560032300646459304L;  
  3.   
  4.     protected   void  doGet(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {  
  5.         response.setContentType("text/html;charset=GBK" );  
  6.           
  7.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );  
  8.         String date = sdf.format(new  Date());  
  9.           
  10.         response.getWriter().println("现在的时间是:"  + date);  
  11.     }  
  12. }  

5、资源映射的配置

     如果要通过web容器访问bundle中的web资源,必须要通过某种方式来告诉OSGi框架,哪些资源可以访问,访问的规则是什么。新增一个名为“plugin.xml”的配置文件,增加以下内容:

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>    
  2. <? eclipse   version = "3.0" ?>    
  3. < plugin >    
  4.     <!-- 配置扩展点 -->   
  5.     < extension   point = "org.eclipse.equinox.http.registry.resources" >   
  6.         <!-- 映射目录资源。alias必须以/开头,不能以/结尾 -->   
  7.         < resource   alias = "/images"   base-name = "WebRoot/images" />   
  8.         < resource   alias = "/pages"   base-name = "WebRoot/pages" />   
  9.     </ extension >   
  10.       
  11.     < extension   point = "org.eclipse.equinox.http.registry.servlets" >    
  12.         <!-- 映射Servlet资源 -->   
  13.         < servlet   alias = "/hello"   class = "com.cjm.web.servlet.HelloWorldServlet"   load-on-startup = "true" />   
  14.           
  15.         <!-- 映射jsp资源 -->   
  16.         < servlet   alias = "/*.jsp"   class = "org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/WebRoot/Pages/" />    
  17.     </ extension >    
  18. </ plugin >    

      通过扩展点向web服务器中注册静态资源和Servlet。    

            alias:在URL中访问的路径。

            basse-name:真正的文件路径。

6、启动项目

     jetty默认的端口是80,如果需要修改该端口,可以在Arguments栏的VM arguments中配置启动参数:

     -Dorg.osgi.service.http.port=8081,如下图:

    启动项目后,可以通过类似以下的路径来访问:

        http://localhost:8081/pages/hello.html
        http://localhost:8081/hello
        http://localhost:8081/index.jsp

猜你喜欢

转载自marsvaadin.iteye.com/blog/1678292