2014-12-04 44 views
1

我遇到問題。我想使用OSGi作爲網站。我對OSGi相當陌生,但我已經閱讀了基礎知識並希望使用框架Equinox。爲此,我從http://eclipse.org/equinox/閱讀了快速入門指南,併購買了書籍「OSGi和Equinox - 創建高度模塊化的Java系統」Web-Container中的OSGi - 請求的資源不可用(JSP文件)

但是回到我的問題。我從Equinox站點下載了bridge.war,並創建了第一個包含servlet的包。激活器是這樣的:

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


public void stop(BundleContext bundleContext) throws Exception { 
    Activator.context = null; 
    httpServiceTracker.close(); 
    httpServiceTracker = null; 
} 

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 { 
    httpService.registerServlet("/index", new StartServlet(), null, null); //$NON-NLS-1$ 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    return httpService; 
} 

public void removedService(ServiceReference reference, Object service) { 
    HttpService httpService = (HttpService) service; 
    httpService.unregister("/index"); //$NON-NLS-1$ 
    super.removedService(reference, service); 
} 
} 

而且我的servlet是這樣的:

保護無效的doGet(HttpServletRequest的請求,HttpServletResponse的響應)拋出了ServletException,IOException異常{ performTask(請求,響應); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, 
    IOException { 
performTask(request, response); 
} 

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, 
    IOException { 
RequestDispatcher RequestDispatcherview = request.getRequestDispatcher("test.jsp"); 
RequestDispatcherview.forward(request, response); 
} 

JSP文件是在文件夾/的WebContent /這在捆綁。 如果我部署束引入Bridge.war,然後嘗試在瀏覽器中打開網站,我總是得到如下:

enter image description here

我不知道我可以配置我的Servlet或者我的活化劑,他們會找到jsp文件。 我試圖將JSP文件移動到bridge.war中,但它不能幫助我防止此問題。

我想我必須在任何地方註冊jsp文件(可能與httpService.registerResources(arg0, arg1, arg2);),但我不知道我是如何正確執行此操作的。

我希望你能幫助我。

+0

你已經在「/ index」上下文中註冊了你的servlet,你不應該在你的URL的某個地方有「index」嗎? – 2014-12-05 11:57:44

+0

如果我打開http:// localhost:8080/bridge/index,servlet將被激活,並且在servlet中,我嘗試將請求發送到test.jsp,但servlet無法找到jsp文件。 – Baalthasarr 2014-12-05 14:20:06

+0

另一方面,將您的Web應用程序部署到OSGi容器中。要麼創建你自己的捆綁設置,要麼使用Apache Karaf這樣的OSGi容器。 – 2014-12-05 21:45:23

回答

1

我終於解決了我的問題。我使用了擴展點。這是我的plugin.xml:

<plugin> 
    <extension point="org.eclipse.equinox.http.registry.httpcontexts"> 
     <httpcontext id="html"> 
      <resource-mapping path="/WebContent/static"/> 
     </httpcontext> 
    </extension> 
    <extension point="org.eclipse.equinox.http.registry.resources"> 
     <resource alias="/webApp" httpcontextId="html"/> 
    </extension> 

    <extension point="org.eclipse.equinox.http.registry.httpcontexts"> 
     <httpcontext id="jsp"> 
      <resource-mapping path="/WebContent/dynamic"/> 
     </httpcontext> 
    </extension> 
    <extension point="org.eclipse.equinox.http.registry.servlets"> 
     <servlet alias="/webApp/*.jsp" class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory" httpcontextId="jsp"/> 
     <servlet alias="/webApp/Login" class="webfiles.servlets.Login"/> 
    </extension> 
</plugin> 
在激活

我的啓動和停止方法正在尋找這樣的:

開始:

@Override 
public void start(BundleContext bundleContext) throws Exception { 
    super.start(bundleContext); 
    Activator.context = bundleContext; 

    Bundle jettBundle = Platform.getBundle("org.eclips.equinox.http.jetty"); 
    Bundle httpRegistryBundle = Platform.getBundle("org.eclipse.equinox.http.registry"); 
    try { 
     jettBundle.start(); 
     httpRegistryBundle.start(); 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 
} 

停止:

@Override 
public void stop(BundleContext bundleContext) throws Exception { 
    super.stop(bundleContext); 
    Activator.context = null; 

    Bundle jettBundle = Platform.getBundle("org.eclips.equinox.http.jetty"); 
    Bundle httpRegistryBundle = Platform.getBundle("org.eclipse.equinox.http.registry"); 
    try { 
     jettBundle.stop(); 
     httpRegistryBundle.stop(); 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 
} 

隨着這種配置我可以使用

RequestDispatcher dispatcher = req.getRequestDispatcher("/webApp/start.jsp"); 
dispatcher.forward(req, resp); 

在我的Servlet中沒有問題。

我希望我能幫助別人解決同樣的問題。