首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Eclipse Equinox OSGi Web 應用配置完整指南

     編輯:Java 相關知識

環境需求

  • Eclipse 3.3.1+ (3.2.0 以上均可)
  • 目標:建立支援靜態資源、Servlet、JSP 的 OSGi Web 應用

一、支援靜態頁面與 Servlet

1. 建立 Plug-in 專案

  • 專案名:net.bjzhanghao.osgi.test
  • 勾選 This plug-in is targeted to run with Eclipse
  • 下一步勾選 Generate an activator

2. 配置 MANIFEST.MF 依賴

Require-Bundle: org.eclipse.equinox.http.jetty,
 org.eclipse.equinox.http.servlet,
 org.mortbay.jetty,
 org.apache.commons.logging,
 javax.servlet,
 org.eclipse.equinox.http.registry

3. 建立 Web 資源目錄

專案根目錄下建立 web_files/,放入 index.html。

4. 定義 plugin.xml 擴展點

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
    <extension point="org.eclipse.equinox.http.registry.resources">
        <resource alias="/web" base-name="/web_files"/>
    </extension>
</plugin>

若 MANIFEST.MF 報錯,在 Bundle-SymbolicName 後加 ;singleton:=true。

5. 啟動配置

  1. Run → Open Run Dialog → OSGi Framework 新建配置
  2. 點 Deselect All 清空
  3. 勾選 Workspace 下的專案
  4. 點 Add Required Bundles 自動解析依賴
  5. Debug 啟動

6. 驗證

瀏覽器訪問 https://localhost/web/index.html 應顯示靜態頁面。

二、支援 Servlet

7. 建立 Servlet 類

public class ExampleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        resp.getWriter().println("Hello from OSGi Servlet!");
    }
}

8. plugin.xml 註冊 Servlet

<extension point="org.eclipse.equinox.http.registry.servlets">
    <servlet alias="/exampleServlet" 
             class="net.bjzhanghao.osgi.example.servlet.ExampleServlet"/>
</extension>

9. 重啟驗證

訪問 https://localhost/exampleServlet 應看到輸出。

三、支援 JSP(關鍵步驟)

10. 建立 JSP 檔案

在 web_files/ 下建立 index.jsp。

11. 新增 JSP 相關依賴

Require-Bundle: org.eclipse.equinox.jsp.jasper,
 org.apache.jasper,
 org.eclipse.equinox.jsp.jasper.registry,
 javax.servlet.jsp,
 org.apache.commons.el,
 org.eclipse.equinox.http.helper,
 org.eclipse.osgi,
 org.eclipse.osgi.services

注意:org.eclipse.equinox.http.helper 需從 CVS (/cvsroot/eclipse/equinox-incubator) 取得,非標準發布包內含。

12. 修改 Activator 註冊 JSP Servlet

public class Activator implements BundleActivator {
    private ServiceTracker httpServiceTracker;
    private static final String JSP_CONTEXT = "/jsps";
    private static final String JSP_FOLDER = "/web_files";

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

    public void stop(BundleContext context) throws Exception {
        httpServiceTracker.close(); // 原文有誤,應為 close()
    }

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

        public Object addingService(ServiceReference reference) {
            HttpService httpService = (HttpService) context.getService(reference);
            try {
                HttpContext commonContext = new BundleEntryHttpContext(context.getBundle(), JSP_FOLDER);
                httpService.registerResources(JSP_CONTEXT, "/", commonContext);

                Servlet adaptedJspServlet = new ContextPathServletAdaptor(
                    new JspServlet(context.getBundle(), JSP_FOLDER), JSP_CONTEXT);
                httpService.registerServlet(JSP_CONTEXT + "/*.jsp", adaptedJspServlet, null, commonContext);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return httpService;
        }

        public void removedService(ServiceReference reference, Object service) {
            HttpService httpService = (HttpService) service;
            httpService.unregister(JSP_CONTEXT);
            httpService.unregister(JSP_CONTEXT + "/*.jsp");
            super.removedService(reference, service);
        }
    }
}

13. 更新啟動配置並驗證

在啟動配置中額外勾選 org.eclipse.equinox.http.helper,重新 Debug。訪問 https://localhost/jsps/index.jsp 驗證 JSP 執行。

常見坑點總結

  • 依賴解析失敗:確認所有 Bundle 版本相容,必要時指定版本區間。
  • JSP 編譯錯誤:Jasper 需要 JDK(而非 JRE)執行環境,Eclipse 預設使用 JDK 通常無問題。
  • 靜態資源 404:檢查 alias 與 base-name 路徑大小寫,Linux 區分大小寫。
  • ServiceTracker 生命週期:stop() 中務必呼叫 tracker.close() 而非 open()(原文筆誤)。

此配置示範了 Equinox HTTP Service 的核心用法:資源映射、Servlet 註冊、JSP 引擎整合。掌握此基礎,可進一步建構模組化的 OSGi Web 應用系統。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved