zoukankan      html  css  js  c++  java
  • 将web容器置于OSGi框架下进行web应用的开发

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

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


    2. index.jsp文件的内容如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
    <!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=UTF-8">  
    <title>Insert title here</title>  
    </head>  
    <body>  
        <img src="images/08.png" width="200" height="150">  
    </body>  
    </html>


    3、MANIFEST.MF文件的内容如下:

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: osgi_jetty
    Bundle-SymbolicName: osgi_jetty;singleton:=true
    Bundle-Version: 1.0.0
    Bundle-Activator: com.javaworld.samples.osgi_jetty.Activator
    Bundle-RequiredExecutionEnvironment: JavaSE-1.6
    Import-Package: javax.servlet;version="2.6.0",
     javax.servlet.http;version="2.6.0",
     org.osgi.framework;version="1.3.0"
    Require-Bundle: org.eclipse.equinox.http.registry;bundle-version="1.1.300",
     org.eclipse.equinox.jsp.jasper.registry;bundle-version="1.0.300",
     org.eclipse.equinox.http.jetty;bundle-version="3.0.100"

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

    4、HelloServlet.java文件的内容如下:

    package com.javaworld.samples.osgi_jetty;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloServlet extends HttpServlet {  
        private static final long serialVersionUID = -5560032300646459304L;  
      
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            response.setContentType("text/html;charset=GBK");  
              
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            String date = sdf.format(new Date());  
              
            response.getWriter().println("现在的时间是:" + date);  
        }  
    }  


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

    <?xml version="1.0" encoding="UTF-8"?>   
    <?eclipse version="3.0"?>   
    <plugin>   
        <extension point="org.eclipse.equinox.http.registry.resources">  
            <resource alias="/images" base-name="WebRoot/images"/>  
            <resource alias="/pages" base-name="WebRoot/pages"/>  
        </extension>  
          
        <extension point="org.eclipse.equinox.http.registry.servlets">   
            <servlet alias="/hello" class="com.javaworld.samples.osgi_jetty.HelloServlet" load-on-startup="true"/>  
            <servlet alias="/*.jsp" class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/WebRoot/Pages/"/>   
        </extension>   
    </plugin>   


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


    附件:osgi_jetty.zip

  • 相关阅读:
    关于Windows7
    我家妞妞长大了
    我家乡的美景
    WinCE开发的语言思考
    坐看肖大师的精彩表演
    Windows Embedded Compact 7试用笔记(1)——新变化
    数据库介绍1(体系结构、基本管理)
    数据库介绍(MySQL安装 体系结构、基本管理)
    shell编程基础简述
    数据库介绍1MySQL安装
  • 原文地址:https://www.cnblogs.com/eastson/p/3615984.html
Copyright © 2011-2022 走看看